Difference between revisions of "/dev/null discards unwanted output"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
(Typo and adding syntax for redirecting both output streams)
m (Text replacement - "<source lang="bash">" to "<syntaxhighlight lang="bash" >")
Line 16: Line 16:
 
==Example==
 
==Example==
 
Try searching user vivek in [[/etc/passwd]] file:
 
Try searching user vivek in [[/etc/passwd]] file:
<source lang="bash">grep vivek /etc/passwd && echo "Vivek found" || "Vivek not found"</source>
+
<syntaxhighlight lang="bash" >grep vivek /etc/passwd && echo "Vivek found" || "Vivek not found"</source>
 
Sample outputs:
 
Sample outputs:
 
<pre>vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash
 
<pre>vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash
Line 22: Line 22:
 
</pre>
 
</pre>
 
To ignore actual output and just display the message, modify your command as follows:
 
To ignore actual output and just display the message, modify your command as follows:
<source lang="bash">grep vivek /etc/passwd >/dev/null && echo "Vivek found" || "Vivek not found"</source>
+
<syntaxhighlight lang="bash" >grep vivek /etc/passwd >/dev/null && echo "Vivek found" || "Vivek not found"</source>
 
Sample outputs:
 
Sample outputs:
 
<pre>Vivek found</pre>
 
<pre>Vivek found</pre>
 
Consider the following example from [[The exit status of a command|the exit status]] chapter. The output of '''grep "^$username" $PASSWD_FILE > /dev/null''' is send to /dev/null where it is ignored by the shell.  
 
Consider the following example from [[The exit status of a command|the exit status]] chapter. The output of '''grep "^$username" $PASSWD_FILE > /dev/null''' is send to /dev/null where it is ignored by the shell.  
<source lang="bash">#!/bin/bash
+
<syntaxhighlight lang="bash" >#!/bin/bash
 
# set var  
 
# set var  
 
PASSWD_FILE=/etc/passwd
 
PASSWD_FILE=/etc/passwd

Revision as of 22:49, 29 March 2016

← Empty file creationHomehere documents →

All data written on a /dev/null or /dev/zero special file is discarded by the system. Use /dev/null to send any unwanted output from program/command and syntax is:

command >/dev/null

This syntax redirects the command standard output messages to /dev/null where it is ignored by the shell. OR

command 2>/dev/null

This syntax redirects the command error output messages to /dev/null where it is ignored by the shell. OR

command &>/dev/null

This syntax redirects both standard output and error output messages to /dev/null where it is ignored by the shell.


Example

Try searching user vivek in /etc/passwd file: <syntaxhighlight lang="bash" >grep vivek /etc/passwd && echo "Vivek found" || "Vivek not found"</source> Sample outputs:

vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash
Vivek found

To ignore actual output and just display the message, modify your command as follows: <syntaxhighlight lang="bash" >grep vivek /etc/passwd >/dev/null && echo "Vivek found" || "Vivek not found"</source> Sample outputs:

Vivek found

Consider the following example from the exit status chapter. The output of grep "^$username" $PASSWD_FILE > /dev/null is send to /dev/null where it is ignored by the shell. <syntaxhighlight lang="bash" >#!/bin/bash

  1. set var

PASSWD_FILE=/etc/passwd

  1. get user name

read -p "Enter a user name : " username

  1. try to locate username in in /etc/passwd

grep "^$username" $PASSWD_FILE > /dev/null

  1. store exit status of grep
  2. if found grep will return 0 exit stauts
  3. if not found, grep will return a nonzero exit stauts

status=$?

if test $status -eq 0 then echo "User '$username' found in $PASSWD_FILE file." else echo "User '$username' not found in $PASSWD_FILE file." fi</source> ← Empty file creationHomehere documents →