Shell Script If Statement

 Shell Script If Statement


If Statement

  • In shell script, if statement is used to execute a single condition or rule
  • If the condition is true, then true statements will be executed otherwise false statements will be executed.

Syntax

        if [ condition ]
        then
               true statements
        else
               false statements
        fi

Note
  • The keyword fi should be used to complete the if syntax.

Example

(p1.sh)

Source Code

    echo "--------------------------------"
    echo -e "\t If Statement Example"
    echo "--------------------------------"
    echo "Enter a number: "
    read n
# get a remainder of base 2 using c style code
    r=$((n%2))
# find the odd or even number using remainder option
    if [ $r -eq 0 ]
    then
        echo "This is Even Number"
    else
        echo "This is Odd Number"
    fi

Output

























Video Demo





Comments

Popular posts from this blog

For Loop in Bash Script (Shell Scripting)

Oppo F27 Pro Plus5g - Price, Specs

Vivo V30 Pro 5G