Bash if statement

Check if first augment to script is –help

if [ "$1" = "--help" ]; then
    echo "$0 --help = shows help"
    exit 0
fi

Check if a file exists and is executable

if [ -f "/usr/bin/curl" ]; then
    echo "curl exists"
fi

Check if a binary file exists and is executable

if [ -x "/usr/bin/curl" ]; then
    echo "curl exists and executable"
fi

Check if previous command was sucessfull or not

if [ "$?" != 0 ]; then
    echo failed
    exit 1
fi

Check if variabe set or not

if [ -z ${USER} ]; then
    echo "\$USER not set"
else
    echo "\$USER =  $USER"
fi

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *