Output redirection meaning


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output redirection meaning
# 1  
Old 05-17-2010
Output redirection meaning

Hi,
Can anyone please explain the details of the code below :-
Code:
ls /etc/*.txt > /dev/null 2>&1

# 2  
Old 05-17-2010
Both, STDERR (2) and STDOUT (1) are redirected to nowhere. It's practical when you just want to check the return status of preceding command and don't want to see any output e.g. STDOUT or STDERR.

Edit: Small improvement of my previous answer for better understanding:
Code:
ls /etc/*.txt > /dev/null 2>&1

Redirect STDOUT to /dev/null respectively don't show output; redirect STDERR to STDOUT

Last edited by pseudocoder; 05-17-2010 at 06:29 PM..
# 3  
Old 05-17-2010
Quote:
Originally Posted by pseudocoder
Both, STDERR (2) and STDOUT (1) are redirected to nowhere. It's practical when you just want to check the return status of preceding command and don't want to see any output e.g. STDOUT or STDERR.

Edit: Small improvement of my previous answer for better understanding:
Code:
ls /etc/*.txt > /dev/null 2>&1

Redirect STDOUT to /dev/null respectively don't show output; redirect STDERR to where STDOUT is going

Hi,
Yes, I want to check the return value of a command in UNIXX shell. But, some tools say "Ambiguos Redirection". ? Any other elegant way ?
# 4  
Old 05-17-2010
Quote:
Originally Posted by angshuman_ag
But, some tools say "Ambiguos Redirection". ?
Which tools for example? Do you run your failing command from shell or from a script?
# 5  
Old 05-17-2010
Quote:
Originally Posted by pseudocoder
Which tools for example? Do you run your failing command from shell or from a script?
I think they are custom 3rd party tools which are present as part of OS installation. Some tools are returning long strings. So suppose I want to grep on a string like this -

Code:
"Status: XXXX"

where XXXX denotes number. How do I do that ? And, if that particular string is matched, I would want to do some operation.
# 6  
Old 05-17-2010
I think that this is what pseudocoder would have meant by the return value:
Code:
$ ls /etc/*.txt #> /dev/null 2>&1
ls: /etc/*.txt: not found
$ ls /etc/*.txt > /dev/null 2>&1
$ echo $?
1

Where a non-zero value returned equates to an error...
# 7  
Old 05-18-2010
curleb: yep Smilie
Code:
$ ls
abc.dat
$ ls abc.dat > /dev/null 2>&1
$ echo $?
0
$ ls 123.dat > /dev/null 2>&1
$ echo $?
1
$



---------- Post updated at 11:40 ---------- Previous update was at 11:35 ----------

Quote:
Originally Posted by angshuman_ag
Some tools are returning long strings. So suppose I want to grep on a string like this -

Code:
"Status: XXXX"

where XXXX denotes number. How do I do that ? And, if that particular string is matched, I would want to do some operation.
We would need the complete output of that particular command, than we could offer you an appropriate awk or cut command, which will e.g. use ":" as field delimiter character
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop output redirection

Hi All, i have below for loop of which i am trying to redirect output in a file: for i in `/usr/sbin/ifconfig -a | awk '/flags/ {print $1}' | grep -v lo | sed 's/://g'` do ifconfig $i dhcp status done >> /tmp/logfile but instead the output is appearing as stdout on screen rather than... (12 Replies)
Discussion started by: omkar.jadhav
12 Replies

2. Shell Programming and Scripting

output redirection

Hi all I was wondering if there was a slicker way of doing this without the file - awk '{print $2}' FS=":" "${FILE}" > "${TMPFILE}" { read M_GRP_ID || m_fail 1 "Error: Read failed 1 (${FUNCNAME})" read M_GRP_WAIT || m_fail 1 "Error: Read failed 2 (${FUNCNAME})" }... (6 Replies)
Discussion started by: steadyonabix
6 Replies

3. Shell Programming and Scripting

Redirection of ls -l output

Hi I am making a script where i want to redirect the output of ls -l to a file Example #ls -l fil1.txt > /opt/temp/a.txt ac: No such file or directory I want to capture output of this command like here output is ac: No such file or directory can anyone help (4 Replies)
Discussion started by: anish19
4 Replies

4. UNIX for Dummies Questions & Answers

Output redirection

Hello i am trying to write a script that will redirect the output to a certain file. Here is the code so far: #!/bin/bash ps -e | sort | more > psfile When I execute the script nothing happens since i assume the output was redirected to the file called psfile. When I try to look at the... (1 Reply)
Discussion started by: mfruiz34
1 Replies

5. Shell Programming and Scripting

Output redirection

We have an application here that does some table queries and then prints the result on screen. I do not have the code of this application (which i will just call "queryCommand"), but what it does is that you call it with some parameters and it prints some info about the query and then the... (5 Replies)
Discussion started by: jolateh
5 Replies

6. Shell Programming and Scripting

Redirection output

Hi there I have a script that runs but it outputs everything onto the screen instead of a file. I've tried using the > outputfile.txt however all it does is dump the output to the screen and creates an outputfile.txt but doesn't put anything in that file. Any help would be appreciated ... (6 Replies)
Discussion started by: kma07
6 Replies

7. Shell Programming and Scripting

redirection and output

I'm redirecting the output of a command to a logfile, however, if the user is on a terminal I would also like the output to be displayed on the screen. tar tvf some_tarfile >Logfile if the user is on a term then have the output to the Logfile and also be displayed on the screen at the same... (2 Replies)
Discussion started by: nck
2 Replies

8. UNIX for Advanced & Expert Users

what is the meaning of the following redirection

Hello bash experts., I was doing some experimentations to better understand the following redirection., $Output = `sh -c \"$Cmd 2>&1 1>&2\"`; It is actually the line used in one of the perl program, but the technique i am trying to understand is related to shell scripting. So is there... (5 Replies)
Discussion started by: thegeek
5 Replies

9. UNIX for Dummies Questions & Answers

Output file redirection

Suppose I have a file named a When I write cat a>a The following error message is displayed cat: a: input file is output file and my file a is truncated to zero size. Also the exit status of the last command is 1 Can someone tell me what actually happens when I do so? (1 Reply)
Discussion started by: aagajaba
1 Replies

10. Solaris

netstat -an -- meaning of the output

Dear Experts, I put below command- could you please describe the outputs column- let me describe some them- col_1: (10.131.60.48.55880) The IP address of the local computer and the port number being used for this particular connection appear in the Local Address column. col_2:... (3 Replies)
Discussion started by: thepurple
3 Replies
Login or Register to Ask a Question