How do i use the if conditional statements in bash shell scripts?



  • The structure goes as follows:

    if condition; then
       action
       action
    fi
    

    For example, if the correct number of parameters is less than one print a usage message. The $# is the total number of parameters given. -lt mean less than.

    if [ $# -lt 1 ];then
       echo "You must specify a directory name"
       echo "Usage: $0 <directoryname>"
       exit 1
    fi
    

    Running the script.

    $ ./dirsize.sh
    You must specify a directory name
    Usage: ./dirsize.sh <directoryname>
    

Log in to reply
 

© Lightnetics 2024