What does "who | grep $1" command do in the shell script?



I am learning shell programming from the very basics using the book called Beginning Linux Programming (4th Edition). I am confused by this script with an until-clause:


until who | grep "$1" > /dev/null
   sleep 60

# Now ring the bell and announce the unexpected user.

echo -e '\a'
echo "***** $1 has just logged in *****"

exit 0

My quesiton is what is who | grep "$1" > /dev/null used for here? Why redirect the grep output to /dev/null?


The ‘until’ loop is used to test a condition, as you mentioned, and will run all the ‘do|done’ block until the condition present becomes true. In other words, it only executes the code block when the condition present is FALSE, and runs it until it becomes true. The script you are testing is useful for catching a logged in user that you pass as a parameter to the script (hence, the grep “$1”, being $1 a positional parameter). It will sleep for a minute (sleep 60) until that user logs in to the system, and then it will exit the loop and do all the ‘$1 has just logged in’ stuff. The redirection of grep output to /dev/null is used to not display the output of the grep comand (you could have used grep -q “$1” and that will achieve the same effect).

Hope to have clarified your doubts.

Answered By – mguillech

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More