Solved

Output redirection

Posted on 2004-04-05
13
458 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 150 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now