Conditionals
If ... else
if <if-expr>; then
commands
elif <if-expr>; then
commands
else
commands
fi
if <if-expr>; then
commands
fi
if <if-expr>
then
commands
fi
- <if-expr> = <Linux command>
- <if-expr> = [ <test-expr> ]
if [ $# -eq 1 ]; then
echo "There is 1 argument"
fi
if [ $# -eq 1 -a "$1" = "-run" ]; then
echo "The only argument used was -run"
fi
if [ -e "$1" ]; then
echo "File exists"
fi
Case statement
case <varname> in
<value>)
commands;
;;
*)
default-commands;
;;
esac
- Similar to a switch statement in C/C++ and a case statement in Pascal.
case $choice in
alpha)
echo "Choice: alpha"
;;
beta)
echo "Choice: beta"
;;
*)
echo "case: default"
;;
esac
Select statement
select <varname> in <list of values>; do
commands;
done
- Prompts the user to make a choice from a list of values.
- Stores the value chosen by the user in the specified variable.
- Redisplays the menu if the user enters an empty line.
- Repeats indefinitely unless an "exit" or "break" command is used.
- Terminates if EOF is read.
select choice in $@; do
echo "Selection: $choice"
break
done
Resources URL:
notes/bash/resources
Sources URL:
notes/bash/sources