1、条件判断式:
    
if [ 条件判断式 ]; then
当条件判断式成立时,可以进行的指令工作内容;
fi

if [ 条件判断式 ]; then
当条件判断式成立时,可以进行的指令工作内容;
else
当条件判断式不成立时,可以进行的指令工作内容;
fi
如果考虑更复杂的情况,则可以使用这个语法:
if [ 条件判断式一 ]; then
当条件判断式一成立时,可以进行的指令工作内容;
elif [ 条件判断式二 ]; then
当条件判断式二成立时,可以进行的指令工作内容;
else
当条件判断式一与二均不成立时,可以进行的指令工作内容;
fi
    shell举例:
read -p "Please input (Y/N): " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK, continue"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh, interrupt!"
else
echo "I don't know what is your choise"
fi


2、利用 case ..... esac 判断

case $变数名称 in
"第一个变数内容")
程式段
;;
"第二个变数内容")
程式段
;;
*)
不包含第一个变数内容与第二个变数内容的其他程式执行段
exit 1
;;
esac

case $1 in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> $0 someword"
;;
*)
echo "Usage $0 {hello}"
;;
esac


3、利用 function 功能
function fname() {
程式段
}
    注意:function 的设定一定要在程式的最前面

function printit(){
echo "Your choice is $1"
}
echo "This program will print your selection !"
case $1 in
"one")
printit 1
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage {one|two|three}"
;;
esac


4、loop    
while [ condition ]
do

程式段落
done
当 condition 条件成立时,就进行循环,直到 condition 的条件不成立才停止
until [ condition ]
do

程式段落
done
当 condition 条件成立时,就终止循环, 否则就持续进行。

while [ "$yn" != "yes" ] && [ "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done


until [ "$yn" == "yes" ] || [ "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done


5、for...do....done

for (( 初始值; 限制值; 执行步阶 ))
do

程式段
done

s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"


for循环不止用在数字的循环,非数字也是可以的:
for $var in con1 con2 con3 ...
do
程式段
done

for animal in dog cat elephant
do
echo "There are ""$animal""s.... "
done


复杂一点的:
# 1. 先看看这个目录是否存在啊?
read -p "Please input a directory: " dir
if [ "$dir" == "" ] || [ ! -d "$dir" ]; then
echo "The $dir is NOT exist in your system."
exit 1
fi
# 2. 开始测试档案啰~
filelist=`ls $dir`
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm "
done