- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Need help with ">&" and "<&"
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 03:45 PM
тАО01-17-2002 03:45 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 03:59 PM
тАО01-17-2002 03:59 PM
Re: Need help with ">&" and "<&"
Try this link from hp docs,
http://docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/B2355-90164/B2355-90164_top.html&con=/hpux/onlinedocs/B2355-90164/00/00/35-con.html&toc=/hpux/onlinedocs/B2355-90164/00/00/35-toc.html&searchterms=redirect%7coutput&queryid=20020117-160428
Hope this helps.
Regds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 04:07 PM
тАО01-17-2002 04:07 PM
Re: Need help with ">&" and "<&"
this is in addition to my earlier post,
http://docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/B2355-90046/B2355-90046_top.html&con=/hpux/onlinedocs/B2355-90046/00/00/81-con.html&toc=/hpux/onlinedocs/B2355-90046/00/00/81-toc.html&searchterms=redirect%7coutput&queryid=20020117-161134
Hope this helps.
Regds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 04:09 PM
тАО01-17-2002 04:09 PM
Re: Need help with ">&" and "<&"
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 04:14 PM
тАО01-17-2002 04:14 PM
Re: Need help with ">&" and "<&"
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 04:28 PM
тАО01-17-2002 04:28 PM
SolutionFirst 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 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 04:30 PM
тАО01-17-2002 04:30 PM
Re: Need help with ">&" and "<&"
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 04:35 PM
тАО01-17-2002 04:35 PM
Re: Need help with ">&" and "<&"
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 04:38 PM
тАО01-17-2002 04:38 PM
Re: Need help with ">&" and "<&"
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-17-2002 06:44 PM
тАО01-17-2002 06:44 PM
Re: Need help with ">&" and "<&"
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