The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 04-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Executive summary: You already got the answer. ps lists all processes, and grep checks if the selected process is among them.

Every command in a shell script sets its exit status. The exit status of a pipeline is the exit status of the last command in that pipeline. The exit status is available in the variable $? but the idiomatic way to do this is usually with an if statement, like the one aigles already posted.

if executes the commands you give it as parameters, and takes the then branch if the exit status was zero (meaning success) and the else branch otherwise. If the selected branch is missing, it does nothing. (Some shells allow the then branch to be left out, but this is not entirely portable; in all events, the else branch is optional.)