Shell Logic


Conditional Logic

if [ TEST ]; then
  echo "TEST is true"
elif grep /path/to/file --fixed-strings foo >/dev/null; then
  echo "TEST is false and '/path/to/file' contains 'foo'"
else
  echo "TEST is false and '/path/to/file' does not contain 'foo'"
fi

Shell tests are typically used as the condition (as seen above), but any command that sets the exit code in the expected pattern (zero for true, non-zero for false; as seen above with grep(1)) can be used.


Case Switching Logic

case $INPUT_STRING in
  y)
    echo "Proceeding..."
    ;;
  n)
    echo "Quitting..."
    ;;
  *)
    echo "Please enter y or n only"
    ;;
esac


CategoryRicottone