Conditional Structures

Intro

  • If this happens (if) - Do this If this either happens (elif) - Do this If none of the previous (else) - Do this fi

  • Example:

#!/bin/bash

number=$RANDOM
echo -n "Type a number" # -n doesn't jump, stays in the same line
read player

if test $number -eq $player
then
    echo "Congrats!"
elif test $number -gt $player
then
    echo "Your number is lower"
else
    echo "Your number is greater"
fi

echo "the number of player is $player"
echo "the random number is $number"

Controlling inputs

  • Alternative to switch from other languages

  • Useful for menus in scripts

  • echo "introduce option" read option case $option in 1) echo "option 1" ;; 2) echo "option 2" *) echo "introduce a valid option" ;; esac

Last updated