1、for語法shell
for 變量 in 列表;do
循環體
donebash
2、常見用法
一、for用來遍歷目錄
ssh
#!/bin/bash #Version:0.1 #Author:lovelace #pragram:This scripts is print all files in directory #difine an varibale DIR="/home/scripts/51cto" #All files in directory traversal for f in $(ls $DIR);do echo $f done
輸出結果爲:curl
[root@lovelace for]# ./dir.sh 1.sh 2curl.sh adduer.sh aliquot.sh argument.sh argusum.sh curl.sh dd.sh dirper.sh info.sh info.tt ipcheck.sh jugement.sh netcard.sh sum.sh test.sh The Frist week The Third week
二、for ((初始條件;終止條件;異動項))
do
命令區域
done
ide
#!/bin/bash #Version:0.1 #Author:lovelace #pragram:This pragram is and the sum from 1 to 100 #define an integer declare -i i #loops for ((i=1;i<=10;i=i+1)) do let sum+=1 done echo "The result is:" $sum
輸出結果爲:oop
[root@lovelace for]# ./sorsum.sh The result is: 10
三、for 無窮循環
for ((;1;));do
命令區域
done
url
[root@lovelace for]# cat forover.sh #!/bin/bash for ((;1;));do echo "forever..." done
輸出結果:spa
[root@lovelace for]# ./forover.sh forever... forever... forever... forever... forever... forever... forever...
3、關於break和continuethree
break、continue 同樣能夠運用在for while until select這4中循環中,ip
break :退出循環 提早退出
continue:提早進入下一輪循環 ,continue後面的語句將再也不執行
示例(計算1到100內能被3整除的數之和):
#!/bin/bash #Verson:0.1 #Auther:lovelace #Pragram:This pragram is calculation from 1 to 100 aliquot 3 sum # declare -i i=0 declare -i sum=0 #use loop traverse from 1 to 100 while [ $i -lt 100 ];do let i++ #jugement aliqotu 3 or not if [ $(($i%3)) -eq 0 ];then let sum+=i else continue fi done #print sum echo "from 1 to 100 aliquot 3 sum is $sum"
輸出結果爲:
[root@lovelace for]# ./three.sh from 1 to 100 aliquot 3 sum is 1683
4、再次重提如何生成列表
如何生成列表:
一、整數列表
{1..100} 生存1到100的列表
二、seq
seq 10 1到10
seq 5 10 5到10
seq 5 10 2 返回列表爲6 8 10
三、`ls /etc`
生成列表不僅僅只有咱們列出的這些,實際案例上須要靈活運用
示例:(分別顯示當前系統上全部默認shell中爲bash的用戶和默認爲nologin的用戶)
[root@lovelace for]# cat checkbash.sh #!/bin/bash #Version:0.1 #Author:lovelace #pragram:This scripts is check user bash and print it #取出使用bash的用戶個數 bashline=`grep 'bash$' /etc/passwd | wc -l` #取出使用nologin的用戶個數 nologinline=`grep 'nologin$' /etc/passwd | wc -l` #取出bash用戶列表 bashuser=`grep 'bash$' /etc/passwd | cut -d: -f1` #取出nologin用戶列表 nologin=`grep 'nologin$' /etc/passwd | cut -d: -f1` #遍歷使用bash的用戶並打印出來 for x in $bashuser;do echo "bash users is:$x" done #遍歷使用nologin的用戶並打印出來 for y in $nologin;do echo "nologin users is:$y" done #結果以下 [root@lovelace for]# ./checkbash.sh bash users is:root bash users is:nick bash users is:kale bash users is:user2 bash users is:user3 bash users is:user4 bash users is:user5 bash users is:user6 bash users is:user7 bash users is:user8 bash users is:user9 bash users is:user10 bash users is:mark bash users is:lovelace bash users is:lovetest nologin users is:bin nologin users is:daemon nologin users is:adm nologin users is:lp nologin users is:mail nologin users is:uucp nologin users is:operator nologin users is:games nologin users is:gopher nologin users is:ftp nologin users is:nobody nologin users is:nscd nologin users is:vcsa nologin users is:pcap nologin users is:ntp nologin users is:dbus nologin users is:avahi nologin users is:rpc nologin users is:mailnull nologin users is:smmsp nologin users is:sshd nologin users is:oprofile nologin users is:rpcuser nologin users is:nfsnobody nologin users is:xfs nologin users is:haldaemon nologin users is:avahi-autoipd nologin users is:gdm nologin users is:sabayon nologin users is:jack