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
hiba_tAsked:
Who is Participating?
 
sunnycoderConnect With a Mentor Commented:
Even I do not believe there is such a command which will execute on all shells as syntax for each shell is different ...

> I'm executing the command through a program that will use telnet and execute one command
>line...
I am not clear about your scenario but if you are using telnet, you can run an instance of sh or bash or any other shell you like, before you execute the command ... also you can still call the script in place of the command and it will still be one line
0
 
sunnycoderCommented:
Hi hiba_t,

mycommand 2>&1 tee filename

Sunnycoder
0
 
hiba_tAuthor Commented:
It is not working, the output is displayed on the screen but the file is not created ..
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Droby10Commented:
mycommand | tee myfile

0
 
Droby10Commented:
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.

0
 
ahoffmannCommented:
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
0
 
hiba_tAuthor Commented:
Isn't there a solution for this that would work on most (or all) the shells?
0
 
avizitCommented:
>> 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
0
 
hiba_tAuthor 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..
0
 
sunnycoderCommented:
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
0
 
hiba_tAuthor 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..
0
 
ahoffmannCommented:
why do you not change your command in that way it writes to a file *and* to stderr?
0
 
ahoffmannCommented:
also, following works in any shell:
  /bin/sh -c "mycommand  2>&1 | tee -a filename"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.