#每日Linux小练习#08 Shell Script知识点总结(下)

今天对Script中的循环进行练习,然后使用一些script的调试参数

while

#while
while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
    read -p "Please input YES/yes to stop this program:" yn
done
echo "OK! you input the correct answer"

until

#until
until [ "$yn" == "no" -o "$yn" == "NO" ]
do 
    read -p "Please input no/NO to stop this program:" yn
done
echo "OK! you input the correct answer"

for

#for
function printAnimal() {
    for animal in $@
    do
        echo "$animal"
    done 
}
printAnimal cat dog elephant

user=$(cut -d ':' -f1 /etc/passwd)
for username in $user
do
    echo "$username"
done

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

script调试

sh -x 20150810loop.sh

可以看到完整的执行过程,比如上文代码中的最后一个循环

原文地址:https://www.cnblogs.com/wuqi/p/4724038.html