Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Output redirection

Posted on 2004-04-05
13
Medium Priority
?
503 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

650 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