Link to home
Start Free TrialLog in
Avatar of hiba_t
hiba_t

asked on

Output redirection

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
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi hiba_t,

mycommand 2>&1 tee filename

Sunnycoder
Avatar of hiba_t
hiba_t

ASKER

It is not working, the output is displayed on the screen but the file is not created ..
mycommand | tee myfile

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
Avatar of hiba_t

ASKER

Isn't there a solution for this that would work on most (or all) the shells?
>> 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
Avatar of hiba_t

ASKER

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..
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
Avatar of hiba_t

ASKER

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..
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"