Operating System - HP-UX
1753666 Members
6189 Online
108799 Solutions
New Discussion юеВ

Need help with ">&" and "<&"

 
SOLVED
Go to solution
Vivek_3
Advisor

Need help with ">&" and "<&"

Hi to all,

Please help me to understand following commands. I run following command through a shell script.

1. echo "Test the command without any descriptor"
2. echo "Test the command with 1>&2" 1>&2
3. echo "Test the command with 2>&1" 2>&1
4. echo "test the command with 1>testfile 2>&1" 1>testfile 2>&1
5. echo "test the command with 1>testfile 1>&2" 1>testfile 1>&2

I have no problem with line # 1.

Line 2 and Line 3 print on trminal. My question is that under what circumstance these commands (2nd and 3rd) will behave differently?

Line 4 is creating a file. I don???t understand why? While line 5th is printing on terminal. I don???t know why?

How does ???<&??? command work? Under what circumstance we use this command?

Please help me.

Thanks
9 REPLIES 9
Sanjay_6
Honored Contributor
Vivek_3
Advisor

Re: Need help with ">&" and "<&"

Sanjay,

I understand whatever the document is saying. i am confuse whenever i see '>' or '<' with '&'.

i dont't understand what is the purpose of "<&" and ">&"> and under what condition i can see the effect??

I hope you understand what i want.

Thanks
Sanjay_6
Honored Contributor

Re: Need help with ">&" and "<&"

Hi Vivek,

Mt 2nd post should help you in understanding the &1 etc.

&0 --> Std input
&1 --> Std output
&2 --> Std error

1>&2 is used to log the std output messaged to the std error.

2>&1 is used to log std error to std output

">" is to redirect to and "<" is to redirect from a file/device/input.

Hope this helps.

regds

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Need help with ">&" and "<&"

Hi:

First of all, anything within the double quotes is just data for the echo command and doesn't matter at all.

Cases 2) and 3) will differ in behavior depending upon whether stdout (1) or stderr (2) has been redirected for the entire script or process; that is , my.sh 2>errfile

4. echo "test the command with 1>testfile 2>&1" 1>testfile 2>&1

This one is first redirecting stdout to testfile; then stderr is redirected to stdout - both go to testfile

5. echo "test the command with 1>testfile 1>&2" 1>testfile 1>&2

In this case, the second redirection of stdout '1>&2' wins and since stderr has not been redirected. stdout now is sent to stderr - your terminal.

In all cases, the shell is doing just what you tell it to do.

Last one:
Imagine that you have a file 'myfile' that looks like this:
Line 1
Line 2
Line 3

If you do this command 'line myfile' then no matter how many times you run it, you will only get 'Line 1'. However, lets do this:

exec 5X1=`line<&5`
X2=`line<&5`
X3=`line<&5`

Now successive lines are read into different variables. This woulld also allow you to write a loop reading each line of the file.

I hope I have been clear, Clay


If it ain't broke, I can fix that.
Vivek_3
Advisor

Re: Need help with ">&" and "<&"

Sanjay,

Just trying to understand.

Why would i want to use "1>&2" or "2>&1"? since the default value for both stdout and stderr is terminal. so whatever i do it will print on screen. even if i don't redirect it will print on scrren in any both the cases.

I which case should i use these?


Thanks
Vivek_3
Advisor

Re: Need help with ">&" and "<&"

I am sorry for all typo.

Thanks
Sanjay_6
Honored Contributor

Re: Need help with ">&" and "<&"

Hi Vivek,

the std output is the terminal if the command is run in foreground. that is if you login into the system and run the command.

There are times when we run commands using scripts, say cron jobs. In situations like those, we like to redirect the output of the command to a file for example for later viewing.

We would also like to redirect the error if any to the same file so that we are able to see if there was any error while the command / script was executed.

This is the reason we redirect the output and the std error using the redirectors.

Hope this helps.

regds
Bill Hassell
Honored Contributor

Re: Need help with ">&" and "<&"

Redirecting the two output streams is a fairly important shell concept. It's easier to visualize by separating the 2 components:

1> and 2> (and 3> 4> 5> and so on)
The number is the number of the file descriptor and the > means to redirect. And as mentioned, &1 and &2 (and &3 &4 &5 etc) are the shorthand names for the files.

So 2>&1 means: redirect the output of file 2 (STDERR) into file descriptor 1. Why? Suppose you are running a command in a shell script and you need everything logged into a file (normal output as well as errors), then 2>&1 will do that.

Another practical example is debugging a shell script:

$ sh -x shell.script

On your terminal screen, you see the script output and the trace all interleaved together. But send the results to the printer:

$ sh -x shell.script | lp

and all the trace steps stay on the terminal screen. Why? The -x option sends all trace steps to STDERR, so to get the trace sent to the printer, you must redirect STDERR:

$ sh -x shell.script 2> &1 | lp

The order of redirection is important. Consider the output of these commands:

$ ls /etc/issue /aaa
/aaa not found
/etc/issue

Normal output, but an error message was sent to stderr and both STDOUT and STDERR go to the terminal screen.

$ ls /etc/issue /aaa 1>/dev/null
/aaa not found

STDOUT has been sent to /dev/null so all that is left is the STDERR message.

$ ls /etc/issue /aaa 2>/dev/null
/etc/issue

Now we've redirected STDERR to the bit bucket.

$ ls /etc/issue /aaa 1>/dev/null 2>&1

Oops, no output at all since STDOUT goes to /dev/null and STDERR goes to STDOUT (which is /dev/null)

$ ls /etc/issue /aaa 2>&1 1>/dev/null
/aaa not found

This is where order is very important. STDERR is redirected to the current file represented by STDOUT (the terminal). Then, STDOUT is redirected to /dev/null, but this does not affect STDERR. So redirection changes the current state of the file descriptor to match another, but it doesn't 'connect' the two descriptors together.


Bill Hassell, sysadmin