Decision Making
Last updated
Last updated
#!/bin/bash
# Bash if statement example
read -p "What is your name? " name
if [[ -z ${name} ]]
then
echo "Please enter your name!"
fi if [ expression ]
then
Commands
else
Commands
fi#!/bin/bash
# Bash if statement example
read -p "What is your name? " name
if [[ -z ${name} ]]
then
echo "Please enter your name!"
else
echo "Hi there ${name}"
fiif [ expression ]
then
Commands
elif [ expression ]
commands
else
Commands
fi
#!/bin/bash
read -p "Enter a number: " num
if [[ $num -gt 0 ]] ; then
echo "The number is positive"
elif [[ $num -lt 0 ]] ; then
echo "The number is negative"
else
echo "The number is 0"
ficase $some_variable in
pattern_1)
commands
;;
pattern_2| pattern_3)
commands
;;
*)
default commands
;;
esac
#!/bin/bash
read -p "Enter the name of your car brand: " car
case $car in
Tesla)
echo -n "${car}'s car factory is in the USA."
;;
BMW | Mercedes | Audi | Porsche)
echo -n "${car}'s car factory is in Germany."
;;
Toyota | Mazda | Mitsubishi | Subaru)
echo -n "${car}'s car factory is in Japan."
;;
*)
echo -n "${car} is an unknown car brand"
;;
esac