本篇博客很簡單,看一下shell編程使用到的循環語句,包括for循環,while循環,until循環,for后邊跟一個變量,然后是一個集合,將集合中的東西賦給這個變量,每次循環執行,這跟java中的foreach很像,while循環和if使用同樣的條件判斷,滿足條件執行語句,until和while相反,不滿足條件執行語句,是不是很簡單啊,下面看一下代碼吧。
#!/bin/sh
#for循環最基本的用法
for var in "hello" "xiao ta" "welcome to www.jb51.net"
do
echo -n "$var "
done
echo
#通配符擴展
for var in $(ls *.sh)
do
echo "$var"
done
#while循環,后邊和if一樣跟的都是條件
echo "please input secret"
read secret
while [ "$secret" != "xiao ta" ]
do
echo "try again"
read secret
done
#until循環和while相反,條件為假才執行
echo "please input text"
read text
until [ "$text" = "xiao ta" ]
do
echo "try again"
read text
done
exit 0