?
Solved

Output redirection

Posted on 2004-04-05
13
Medium Priority
?
497 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
0
Comment
Question by:hiba_t
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10758083
Hi hiba_t,

mycommand 2>&1 tee filename

Sunnycoder
0
 

Author Comment

by:hiba_t
ID: 10758191
It is not working, the output is displayed on the screen but the file is not created ..
0
 
LVL 5

Expert Comment

by:Droby10
ID: 10758475
mycommand | tee myfile

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 5

Expert Comment

by:Droby10
ID: 10758558
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10763530
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
 

Author Comment

by:hiba_t
ID: 10763539
Isn't there a solution for this that would work on most (or all) the shells?
0
 
LVL 11

Expert Comment

by:avizit
ID: 10763778
>> 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
 

Author Comment

by:hiba_t
ID: 10763820
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10763986
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
 

Author Comment

by:hiba_t
ID: 10763996
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 450 total points
ID: 10764016
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10766012
why do you not change your command in that way it writes to a file *and* to stderr?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 10766030
also, following works in any shell:
  /bin/sh -c "mycommand  2>&1 | tee -a filename"
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question