您好,登錄后才能下訂單哦!
循環(huán)命令用于將一個命令或一組命令執(zhí)行指定的次數(shù),或者一直執(zhí)行直到滿足某個條件為止。在Bash shell中常用的循環(huán)語句有,for循環(huán),while循環(huán),until循環(huán)
一、For循環(huán)語句
1、For循環(huán)的語法
for var in list
do
commands
done
2、For循環(huán)的流程圖
3、For循環(huán)舉例
1)、輸入一個文件,判斷文件是directory還是file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@localhost test]# cat 3 .sh #!/bin/sh for file in $ 1 do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi done [root@localhost test]# sh 3 .sh /etc/passwd /etc/passwd is a file [root@localhost test]# sh 3 .sh /etc /etc is a directory [root@localhost test]# |
說明:
行3:調(diào)用了位置變量$1
行5-11:使用if語句判斷$1是文件還是目錄
2)、計算一個目錄中有多少個文件
1 2 3 4 5 6 7 8 9 | [root@localhost test]# cat 4 .sh #!/bin/bash # Count= 0 for File in /tmp/*; do
file $File
Count=$[$Count+ 1 ] done echo "Total files: $Count." |
說明:
http://t.163.com/event/info/eventId/-2934105915185819762
http://t.163.com/event/info/eventId/7336578789135698864
http://t.163.com/event/info/eventId/-2757900099931402204
http://t.163.com/event/info/eventId/-6022217269862157430
http://t.163.com/event/info/eventId/-6327288559455055957
http://t.163.com/event/info/eventId/-5434619824903743434
http://t.163.com/event/info/eventId/-7296668920759701419
http://t.163.com/event/info/eventId/-6761754697175518719
http://t.163.com/event/info/eventId/2714712527073182328
http://t.163.com/event/info/eventId/-5002856799661246013
http://t.163.com/event/info/eventId/-4996357046007032207
http://t.163.com/event/info/eventId/1105816084476859073
http://t.163.com/event/info/eventId/-8600093788630276814
http://t.163.com/event/info/eventId/8431790839426740003
http://t.163.com/event/info/eventId/-7330955025813744932
http://t.163.com/event/info/eventId/8644906182003528012
http://t.163.com/event/info/eventId/7423718734365387206
http://t.163.com/event/info/eventId/8142539201743089519
http://t.163.com/event/info/eventId/6894078421345426422
http://t.163.com/event/info/eventId/-6211762240931380686
http://t.163.com/event/info/eventId/-8088822526390658491
http://t.163.com/event/info/eventId/5348067141736717459
http://t.163.com/event/info/eventId/1446077859086446713
http://t.163.com/event/info/eventId/-3875504737289263757
http://t.163.com/event/info/eventId/-7305448483219754978
http://t.163.com/event/info/eventId/-6331199466714658720
http://t.163.com/event/info/eventId/-4025232118219909260
行7:每循環(huán)一次Count的值+1
二、While循環(huán)語句
while命令允許定義要測試的命令,然后只要定義的測試命令返回0狀態(tài)值,則執(zhí)行while循環(huán)中的語句,否則直接退出
1)、while循環(huán)的語法
while test command
do
oter command
done
2)、while循環(huán)的流程圖
3)、while循環(huán)舉例
計算100以內(nèi)整數(shù)的和
1 2 3 4 5 6 7 8 9 | [root@localhost test]# cat 6 .sh #!/bin/sh Sum= 0 Count= 1 while [ $Count -le 100 ]; do
let Sum+=$Count
let Count++ done echo $Sum |
如果用戶的ID號為偶數(shù),則顯示其名稱和shell;對所有用戶執(zhí)行此操作;
1 2 3 4 5 6 7 8 | [root@localhost test]# cat 5 .sh #!/bin/sh while read LINE; do
Uid=`echo $LINE | cut -d: -f3`
if [ $[$Uid% 2 ] -eq 0 ]; then
echo $LINE | cut -d: -f1, 7
fi done < /etc/passwd |
說明:
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。