Bash Programming Cheat Sheet Page 3

ADVERTISEMENT

Bash Programming Cheat Sheet
Page 3 of 4
Logic Con't.
If...then
if [ expression ]
then
commands
fi
If..then...else
if [ expression ]
then
commands
else
commands
fi
If..then...else If...else
if [ expression ]
then
commands
elif [ expression2 ]
then
commands
else
commands
fi
Case select
case string1 in
str1)
commands;;
str2)
commands;;
*)
commands;;
esac
string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double
semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are
executed. This is the default case condition because the asterisk matches all strings.
Iteration (Loops)
for var1 in list
do
commands
done
This executes once for each item in the list. This list can be a variable that contains several words separated by spaces
(such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through
the loop, the variable var1 is assigned the current item in the list, until the last one is reached.
while [ expression ]
18/3/2553

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4