IT 3110 : Advanced System Administration

Command Execution

PATH variable

  • echo $PATH
  • How to edit the path?

Background

  • place a & at end of command
  • keyboard input is disconnected from command and shell doesn't wait for command to finish before it gives prompt back.

Command success

  • $? is set to nonzero if command fails.
    • echo foo
    • echo $?
  • or
    • ping ahsdf -c 1
    • echo $?

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. More if later.

More about &&

Assuming you run a command like:

aa && bb && cc

bb will only run if aa executed successfully (remember the status code)
cc will ony run if both aa and bb executed successfully.

Note that:

a & b & c

Is totally different and will cause a and b to run in the background.

Nohup

  • Allows you to run long jobs unattended
  • Normal children of shell processes are terminated when shell is terminated

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

More on variables later.