IT 3110 : Advanced System Administration

Command Execution


PATH variable


Background


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.


Testing

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

or

    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.


Nohup


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

Variableness

Can save a program as a variable

   f=myfile.txt
   p=cat
   $p $f