溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

bash腳本中如何使用while

發(fā)布時(shí)間:2021-12-20 09:21:43 來源:億速云 閱讀:195 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要為大家展示了“bash腳本中如何使用while”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“bash腳本中如何使用while”這篇文章吧。

腳本編程:
 順序結(jié)構(gòu)
 選擇結(jié)構(gòu)
  if
  case
 循環(huán)結(jié)構(gòu)
  for
  while
  until
  
while循環(huán):適用于循環(huán)次數(shù)未知的場(chǎng)景,要有退出條件
語法:
 while CONDITION; do
   statement
   ...
 done
 


練習(xí):計(jì)算100以內(nèi)所有正整數(shù)的和

[root@localhost ~]# bash sum3.sh
the sum is: 5050
[root@localhost ~]# cat sum3.sh
#!/bin/bash

declare -i i=1
declare -i sum=0

while [ $i -le 100 ]; do
 let sum+=$i
 let i++
done

echo "the sum is: $sum"
[root@localhost ~]#

將小寫轉(zhuǎn)換為大寫

[root@localhost ~]# bash translate.sh
input string:asdf
ASDF
input string:bad
BAD
input string:quit
[root@localhost ~]# cat translate.sh
#!/bin/bash

read -p "input string:" string

while [ $string != 'quit' ]; do
 echo $string | tr 'a-z' 'A-Z'
 read -p "input string:" string
done

[root@localhost ~]#

練習(xí):每隔5秒查看hadoop用戶是否登錄,如果登錄,顯示其登錄并退出;否則,顯示當(dāng)前時(shí)間,并說明hadoop尚未登錄:

[root@localhost ~]# vim checkhadoop.sh
[root@localhost ~]# bash +x checkhadoop.sh
Thu Feb 23 20:53:33 PST 2017
Thu Feb 23 20:53:38 PST 2017
Thu Feb 23 20:53:43 PST 2017
Thu Feb 23 20:53:48 PST 2017
Thu Feb 23 20:53:53 PST 2017
Thu Feb 23 20:53:58 PST 2017
Thu Feb 23 20:54:03 PST 2017
Thu Feb 23 20:54:08 PST 2017
Thu Feb 23 20:54:13 PST 2017
Thu Feb 23 20:54:18 PST 2017
Thu Feb 23 20:54:23 PST 2017
hadoop is logged in
[root@localhost ~]# cat checkhadoop.sh
#!/bin/bash

who | grep "hadoop" &> /dev/null
ret=$?

while [ $ret -ne 0 ] ; do
 sleep 5
 echo "`date`"
 who | grep "hadoop" &> /dev/null
 ret=$?
done

echo "hadoop is logged in"
[root@localhost ~]#

寫一個(gè)腳本:
1) 顯示一個(gè)菜單給用戶:
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
2) 當(dāng)用戶給定選項(xiàng)后顯示相應(yīng)的內(nèi)容;
  
擴(kuò)展:
 當(dāng)用戶選擇完成,顯示相應(yīng)信息后,不退出;而讓用戶再一次選擇,再次顯示相應(yīng)內(nèi)容;除了用戶使用quit;

[root@localhost ~]# bash showdisk.sh
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
your choice:d
disk usage:
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        18G  3.1G   14G  19% /
tmpfs           931M   72K  931M   1% /dev/shm
/dev/sda1       283M   33M  236M  13% /boot
your choice:m
memory usage:
Mem:          1861        452       1409          1         68        153
your choice:s
swap usage:
Swap:         2047          0       2047
your choice:quit
[root@localhost ~]# cat showdisk.sh
#!/bin/bash

cat << EOF
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
EOF

read -p "your choice:" choice

while [ $choice != 'quit' ]; do
case $choice in
d|D)
 echo "disk usage:"
 df -Ph
 ;;
m|M)
 echo "memory usage:"
 free -m | grep Mem
 ;;
s|S)
 echo "swap usage:"
 free -m | grep Swap
 ;;
*)
 echo "unkonw"
 exit 9
 ;;
esac
read -p "your choice:" choice

done
[root@localhost ~]#


循環(huán)控制:

continue
中斷當(dāng)前這一次循環(huán),提前進(jìn)入下一軟循環(huán)

[root@localhost ~]

# bash +x sum4.sh
the sum is: 2550
[root@localhost ~]# cat sum4.sh
#!/bin/bash
let sum=0
let i=0

while [ $i -le 100 ]; do
 let i++
 if [ $[$i%2] -eq 1 ];then
  continue
 fi
 let sum+=$i
done

echo "the sum is: $sum"
[root@localhost ~]#

break
中斷循環(huán),而后執(zhí)行循環(huán)后面的語句;

[root@localhost ~]# cat sum5.sh
#!/bin/bash

declare -i sum=0

for i in {1..10000};do
 let sum+=$i
 if [ $sum -gt 5000 ];then
  break
 fi
 let i++
done
echo "the sum is:$sum"
[root@localhost ~]# bash sum5.sh
the sum is:5050
[root@localhost ~]#

while特殊用法1--無限循環(huán)

[root@localhost ~]# bash while1.sh
file path:hello
hello not exist
file path:/etc/inittab
/etc/inittab exits
file path:quit
[root@localhost ~]# cat while1.sh
#!/bin/bash

while :;do
 read -p "file path:" filepath
 [ $filepath == "quit" ] && break
 if [ -e $filepath ];then
  echo "$filepath exits"
 else
  echo "$filepath not exist"
 fi
done
[root@localhost ~]#

while特殊用法2--按行讀取文件

[root@localhost ~]# cat while2.sh
#!/bin/bash
file=/etc/passwd


while read line;do
 [ `echo $line | awk -F : '{print $7}'` ] && echo $line | awk -F : '{print $1}'
done < $file
[root@localhost ~]# bash while2.sh
root
bin
daemon
adm
lp
sync
shutdown
halt

以上是“bash腳本中如何使用while”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI