#!/bin/bash
for file in $(ls /tmp/test/mytest |grep sh) //for in格式是shell for的基本格式,根js的for in類似
do //循環(huán)開始你就把它當成{
echo $file
done //循環(huán)結(jié)束你就把它當成}
for ((i=0;i10;i++)) //注意是雙小括號,由于受其他語言的影響,很容易搞錯
do
echo -n $i
done
echo \&; //輸出換行
for i in 0 1 2 3 4 5 6 7 8 9
do
echo -n $i
done
echo \
for i in "0 1 2 3 4 5 6 7 8 9" //這個根上面是有區(qū)別的,這個循環(huán)只循環(huán)了一次,雙引號里面只是一個變量
do
echo -n $i
done
exit 0
#!/bin/bash
i=0
while ((i10))
do
echo $i
((i += 1))
done
i=0
while [ $i -lt 10 ] //注意括號內(nèi)側(cè)二邊的空格
do
echo $i
let "i+=1" //加1
done
exit 0
#!/bin/bash
END_CONDITION=end
until [ "$var1" = "$END_CONDITION" ] //讀取的變量根設(shè)定的變量相等時退出循環(huán),不然永遠循環(huán)
do
echo "Input variable #1 "
echo "($END_CONDITION to exit)"
read var1
echo "variable #1 = $var1"
echo
done
exit 0
#!/bin/bash
echo "Input a number #1 "
read num
echo "variable #1 = $num"
if [ $num -lt 60 ] //注意lt前面的-,很容易忘的
then
echo "you are not pass"
elif [ $num -lt 70 ] [ $num -ge 60 ] //多個條件的判斷
then
echo "pass"
elif [[ $num -lt 85 $num -ge 70 ]] //如果放在一起,要注意是雙方括號,不要寫成[ $num -lt 85 $num -ge 70 ]
then
echo "good"
elif (( $num = 100 )) (( $num >= 85 )) //對于有語言基礎(chǔ)的人來說,這種寫法讓人覺得很舒服,不要忘了是雙小括號
then
echo "very good"
else
echo "num is wrong"
fi //if要有結(jié)束標簽的,根XML很像,不閉合,就報錯
exit 0
#!/bin/sh
case $1 in
start)
echo "start ok"
;; //注意一點,要注意是雙分號
stop)
echo "stop ok"
;;
restart)
echo "restart ok"
;;
*)
echo "no param"
;;
esac //注意閉合標簽
exit 0
[root@krlcgcms01 forif]# sh c1.sh stop
stop ok