if结构:
read -p "Please input (Y/N): " yn
if [ "$yn" == "Y" -o "$yn" == "y" ]; thenecho "OK, continue"elif [ "$yn" == "N" -o "$yn" == "n" ]; thenecho "Oh, interrupt!"else
echo "I don't know what your choice is"fi
=====================================================case结构:case $1 in"hello") echo "Hello, how are you ?" ;; "") echo "You MUST input parameters, ex> {$0 someword}" ;; *) # 其实就相当於万用字节,0~无穷多个任意字节之意! echo "Usage $0 {hello}" ;;esac======================================================while 结构:while [ "$yn" != "yes" -a "$yn" != "YES" ] #当条件为真时执行循环do read -p "Please input yes/YES to stop this program: " yndoneecho "OK! you input the correct answer."=======================================================
s=0 # 这是加总的数值变量i=0 # 这是累计的数值,亦即是 1, 2, 3....while [ "$i" != "100" ]do i=$(($i+1)) # 每次 i 都会添加 1 s=$(($s+$i)) # 每次都会加总一次!doneecho"The result of '1+2+3+...+100' is ==> $s"=======================================================
until结构
======================================================for结构:
until [ "$yn" == "yes" -o "$yn" == "YES" ] #当条件为真时退出循环do read -p "Please input yes/YES to stop this program: " yndoneecho "OK! you input the correct answer."
for animal in dog cat elephant
do
echo "There are ${animal}s.... "
done
=====================================================network="192.168.1" # 先定义一个网域的前面部分!for sitenu in $(seq 1 100) # seq 为 sequence(连续) 的缩写之意do # 底下的程序在取得 ping 的回传值是正确的还是失败的! ping -c 1 -w1 ${network}.${sitenu} &> /dev/null && result=0 || result=1 # 开始显示结果是正确的启动 (UP) 还是错误的没有连通 (DOWN) if [ "$result" == 0 ]; then
echo "Server ${network}.${sitenu} is UP."
else
echo "Server ${network}.${sitenu} is DOWN."
fi
done
====================================================
# 1. 先看看这个目录是否存在啊?
read -p "Please input a directory: " dir
if [ "$dir" == "" -o ! -d "$dir" ]; then
echo"The $dir is NOT exist in your system." exit 1
fi# 2. 开始测试文件罗~filelist=$(ls $dir) # 列出所有在该目录下的文件名称for filename in $filelistdo 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
====================================================C语言方式:(双小括号方式--单小括号是执行命令)read -p "Please input a number, I will count for 1+2+...+your_input: " nus=0
for (( i=1; i<=$nu; i=i+1 ))do s=$(($s+$i))done
echo"The result of '1+2+3+...+$nu' is ==> $s"
=====================================================select 结构
echo "What is your favourite OS?"select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do break;doneecho "You have selected $var"
该脚本的运行结果如下:
What is your favourite OS?1) Linux2) Gnu Hurd3) Free BSD4) Other#? 1You have selected Linux