Operating System - HP-UX
1819803 Members
3197 Online
109607 Solutions
New Discussion юеВ

redirecting stdin & stdout

 
SOLVED
Go to solution
u856100
Frequent Advisor

redirecting stdin & stdout

hi all,

I am having trouble getting the 2>&1 ethic to work

I am simply trying :
lsf 2>&1 redirect_test

1- The statement won't work if the file is not previously created (??)
2- If the file is created, the output(s) are still not written

I know this is basic, but I just can't get it to work. (I have also tried an example from the HP certified book - can't get that to work either)

any help will be much appreciated,

thanks

John
chicken or egg first?
3 REPLIES 3
ian Dennison
Regular Advisor
Solution

Re: redirecting stdin & stdout

lsf 2>&1 >>/tmp/filename

2>&1 to capture all output, then the redirection to the appropriate destination.

Share and Enjoy! Ian
Lets do it to them before they do it to us! www.fred.net.nz
Deepak Extross
Honored Contributor

Re: redirecting stdin & stdout

The following works just as well, and is less confusing:
lsf 1>redirect_test 2>redirect_test


Darrell Allen
Honored Contributor

Re: redirecting stdin & stdout

Hi John,

It appears you are trying to redirect standard output (file descriptor 1) and standard error (fd 2) to redirect_test. Bear with me in this explanation.

In Bourne shell and its derivitives (ie Korn and Posix) output to a file can be redirected with #> where # is the file descriptor number (1 = stdout, 2 = stderr, others as defined by the process). The default for # is 1. Therefore 1> is the same as >. So to redirect stdout use >outfile. To redirect stderr use 2>errfile. To redirect stdout and stderr to the same file use:
command >file 2>&1
Note that with 2>&1 stderr (fd 2) is redirected to the same file as stdout (fd 1).

The order in which redirection is specified is important. Consider the following 2 examples:
command >outfile 2>&1
command 2>&1 >outfile

The first example redirects stdout to outfile then redirects stderr to the same file used by stdout (outfile). Thus output and errors are written to outfile.

The second example redirects stderr to the same file used by stdout which since stdout has yet to be redirected is by default the screen. Then stdout is redirected to outfile. The result is that output is written to outfile but errors are written to the screen.

Test the above as follows:
mkdir testdir
cd testdir
ls junk >outfile 2>&1
ls junk 2>&1 >outfile

Finally, to your problem.
lsf 2>&1 redirect_test
redirect_test is being seen as an argument to lsf. It it does not exist, you get the error "redirect_test not found". The error is sent to stderr which is redirected to the same file as stdout and is printed on the screen. If redirect_test exists, the lsf command writes "redirect_test" to stdout (the screen).

The commonly used syntax you need is:
lsf >redirect_test 2>&1

The more fully qualified syntax (with the same results as the commonly used syntax) is:
lsf 1>redirect_test 2>redirect_test

See the man pages for sh-bourne or sh-posix for more info. Also, if you use csh, man csh. csh is a much different animal.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)