IT 3110 : Advanced System Administration

Command Execution

PATH variable


Command success

Exit status

When you write a shell script, it’s a good idea to have your script exit with zero if all is well and a nonzero value if you encounter an error condition.


You can use the exit status ($?) of the cd command in combination with an if statement to do the rm only if the cd was successful:

    cd mytmp
    if (( $? == 0 )); then rm * ; fi


    if cd mytmp; then rm * ; fi

Shortening decisions

The above if statements could also be written as:

   cd mytmp && rm *

The double &, will only go onto the second statement if the first one was able to succeed.


OR statements

Similar to double &, but this one, the rhs runs if the lhs fails

   cmd || printf "%b" "cmd failed. You're on your own\n"

These are different:

   cmd || printf "%b" "FAILED.\n" ; exit 1  #always exits
   cmd || { printf "%b" "FAILED.\n" ; exit 1 ; } #exits on failure


Can save a program as a variable

   $p $f