Bash if

the structure of the bash “if” command is the following,: the word “if “then we have double square brackets (for text condition), then “;” and then the word “then“, after that we have the code that we want to execute if the condition is  true, and eventually we close the if with the word “fi” (reverse order of the word “if”).

For example if want to run code only if we got “run” as the first param to the script:


if [[ $1 = "run" ]]; then
     echo "ruining code"
fi

this will print “running code” only if the first param is “run”.
if we want t have a numerical condition then we use double round brackets.
In such case there is no need for the $ sign before the name of the variable.
For example if we want to check if $number is bigger than 10:


if (( number > 10 )); then
    echo "the number variable is larger than 20"
fi

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s