Unix redirection to '&-'


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unix redirection to '&-'
# 1  
Old 08-31-2007
Java Unix redirection to '&-'

Hi UF family members,

I am intermediate in Unix language and scripting.I know the redirection systems in unix,but the below statement confuses me:

#!/bin/ksh
. $HOME/.profile 2>&-

Actually this is an extract from a unix script which was trying to set the environment variables through the script '.profile'.But the error is redirected to somewhere represented as '&-'

Can anyone give a description of what '&-' means?
Your help is appreciated

With Regards
Dileep Pattayath
# 2  
Old 08-31-2007
you are closing the file descriptor.
# 3  
Old 08-31-2007
Quote:
Originally Posted by Yogesh Sawant
you are closing the file descriptor.
Can you ellaborate?
# 4  
Old 08-31-2007
Closing the error stream

I think the line
Code:
. $HOME/.profile 2>&-

closes the error stream. So, If your shell script writes to the error stream or any command produces errors, it doesnt get displayed on to the terminal. For example,
Code:
exec 2>&-
echo "Does this get printed??"
grep junk filename ##filename doesnot exist

gives,
Code:
rkumar@bdc4reteaix1w: /home/rkumar/unix_forum >./teststream
Does this get printed??

if i change the lines to
Code:
exec 1>&-
echo "Does this get printed??"
grep junk filename

then,
Code:
rkumar@bdc4reteaix1w: /home/rkumar/unix_forum >./teststream
grep: 0652-033 Cannot open filename

# 5  
Old 08-31-2007
Doing ". $HOME/.profile 2>&-" is not a great idea. A better approach would be ". $HOME/.profile 2>/dev/null". FD 2 should be open to something so that writes to STDERR succeed. (And the output will still be discarded.) A program might decide to abort if it can't write to STDERR.
# 6  
Old 08-31-2007
Quote:
Originally Posted by ranj@chn
I think the line
Code:
. $HOME/.profile 2>&-

closes the error stream. So, If your shell script writes to the error stream or any command produces errors, it doesnt get displayed on to the terminal. For example,
Code:
exec 2>&-
echo "Does this get printed??"
grep junk filename ##filename doesnot exist

gives,
Code:
rkumar@bdc4reteaix1w: /home/rkumar/unix_forum >./teststream
Does this get printed??

if i change the lines to
Code:
exec 1>&-
echo "Does this get printed??"
grep junk filename

then,
Code:
rkumar@bdc4reteaix1w: /home/rkumar/unix_forum >./teststream
grep: 0652-033 Cannot open filename


Yes,you are right.Thank you so much for such an ellaborate and vivid description.

Thanks for your time and effort

With Regards
Dileep
# 7  
Old 08-31-2007
Quote:
Originally Posted by Perderabo
Doing ". $HOME/.profile 2>&-" is not a great idea. A better approach would be ". $HOME/.profile 2>/dev/null". FD 2 should be open to something so that writes to STDERR succeed. (And the output will still be discarded.) A program might decide to abort if it can't write to STDERR.
Yes,I agree with your idea.Thanks for your reply

With Regards
Dileep
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

>& redirection not working within csh script

I'm having a strange problem with basic >& output redirection to a simple log file in csh. When I run this particular output redirection on the command line, it works, but then when I run the same output redirection command >& in my c shell script, I get a blank log file. Nothing is output to the... (5 Replies)
Discussion started by: silencio
5 Replies

2. UNIX for Dummies Questions & Answers

redirection in unix

how to redirect a output value to a file (1 Reply)
Discussion started by: pratima.kumari
1 Replies

3. Shell Programming and Scripting

Help with Output Redirection of a Unix Shell Script

Hi Guys, I have a script for which the stdout and stderr should be redirected to a log file, they should not be printed on the screen. Could you please let me know the procedure to redirect the output of the script to a log file. Thanks in advance. --- Aditya (5 Replies)
Discussion started by: chaditya
5 Replies

4. Shell Programming and Scripting

Unix shell output redirection help

Hi all, Actually i need to know whether there is any way to redirect the output of shell operations into any file without pipe . Actually my problem is , i run some command & its result is displayed on shell after some calculations on shell, so if i redirect its output to file, it is not... (5 Replies)
Discussion started by: sarbjit
5 Replies

5. Programming

Java with Unix (Redirection + Piping)

Hi, To explain this question I will have to go into a bit of detail. I hope you don't mind. currently I have a log handler (an already compiled c++ version) and what it does is makes a log file and writes all the unix output (echo, etc) of a script to that log file. To me the log_handler is... (3 Replies)
Discussion started by: fluke_perf
3 Replies

6. Filesystems, Disks and Memory

Unix command redirection

Hi all,, Is there any way to redirect the command o/p directaly to a memory location instead of redirecting it to the file?? (1 Reply)
Discussion started by: swap007
1 Replies

7. UNIX for Dummies Questions & Answers

redirection in unix, '<' as opposed to '>'

Greetings, When directing in unix, symbol > means saving. E.g. I can save ls command output into mama like this: ls -f > mama Could someone give me a real example of how the opposite, i.e. symbol < is used?. Could not find its counterpart in Windows (I seem to learn better when i see... (4 Replies)
Discussion started by: alikun
4 Replies

8. IP Networking

sharing of IP address for load sharing avoiding virtual server & redirection machine

I have RedHat 9.0 installed on three of my servers (PIII - 233MHz) and want that they share a common IP address so that any request made reaches each of the servers. Can anyone suggest how should I setup my LAN. I'm new to networking in Linux so please elaborate and would be thankful for a timely... (2 Replies)
Discussion started by: Rakesh Ranjan
2 Replies

9. UNIX Desktop Questions & Answers

what is the difference between Unix & linux, what are the advantages & disadvantages

ehe may i know what are the difference between Unix & Linux, and what are the advantages of having Unix as well as disadvantages of having Unix or if u dun mind i am dumb do pls tell me what are the advantages as well as the disadvantages of having linux as well. thanks (1 Reply)
Discussion started by: cybertechmkteo
1 Replies
Login or Register to Ask a Question