Bash if statement
Check if first augment to script is –help
1 2 3 4 |
if [ "$1" = "--help" ]; then echo "$0 --help = shows help" exit 0 fi |
Check if a file exists and is executable
1 2 3 |
if [ -f "/usr/bin/curl" ]; then echo "curl exists" fi |
Check if a binary file exists and is executable
1 2 3 |
if [ -x "/usr/bin/curl" ]; then echo "curl exists and executable" fi |
Check if previous command was sucessfull or not
1 2 3 4 |
if [ "$?" != 0 ]; then echo failed exit 1 fi |
Check if variabe set or not
1 2 3 4 5 |
if [ -z ${USER} ]; then echo "\$USER not set" else echo "\$USER = $USER" fi |