We help IT Professionals succeed at work.

Output redirection

hiba_t
hiba_t asked
on
Medium Priority
522 Views
Last Modified: 2011-09-20
I'm using a program that outputs to stderr. I need to run this program in unix in a command line and then have the output displayed on screen (as usual) also I need the output to be in a file...

Is there a way to do this in one command line?

Thanks,
Hiba_t
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

Commented:
Hi hiba_t,

mycommand 2>&1 tee filename

Sunnycoder

Author

Commented:
It is not working, the output is displayed on the screen but the file is not created ..

Commented:
mycommand | tee myfile

Commented:
sorry, read stdout instead of stderr (sunnycoder missed it by one pipe).

mycommand 2>&1 | tee filename

note that this is bourne syntax, you will need to be running sh or bash.

hmm, all given solutions work, somewhere, somehow

hiba_t, which shell are you using?
 sh$   mycommand  2>&1 | tee -a filename
csh% mycommand |& tee -a filename

Author

Commented:
Isn't there a solution for this that would work on most (or all) the shells?

Commented:
>> Isn't there a solution for this that would work on most (or all) the shells?
I _think_ not,  but if you can write it in a script

and put #!/usr/bin/csh etc etc and put that as first line then you can run the script from within any shell and it would work ( provided you have the repective shells present in the system)

-Abhijit

Author

Commented:
Thanks, but a script wouldn't work for me because I need to execute the command in one command line and the command parameters changes from time to time..
CERTIFIED EXPERT
Top Expert 2006

Commented:
you can still form a script

-------------------------------------------------------------
#!/bin/sh

my_command $1 $2 $3 2>&1 | tee $4
------------------------------------------------------------

call this script with same arguments as your command (here it will accept three arguments first three arguments denoted by $1, $2 and $3)  and an additional argument specifying the filename ($4)

suppose you name the above script as test.sh then command

test.sh a b c d

is same as

my_command a b c 2>&1 | tee d

Author

Commented:
But I can't create the script, since I'm executing the command through a program that will use telnet and execute one command line...

I need a to use commands that would work on most shells..
CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
why do you not change your command in that way it writes to a file *and* to stderr?
also, following works in any shell:
  /bin/sh -c "mycommand  2>&1 | tee -a filename"
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.