這篇文章主要為大家展示了“bash腳本中如何使用while”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習一下“bash腳本中如何使用while”這篇文章吧。
創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的克拉瑪依網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
腳本編程:
順序結(jié)構(gòu)
選擇結(jié)構(gòu)
if
case
循環(huán)結(jié)構(gòu)
for
while
until
while循環(huán):適用于循環(huán)次數(shù)未知的場景,要有退出條件
語法:
while CONDITION; do
statement
...
done
練習:計算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 ~]#
練習:每隔5秒查看hadoop用戶是否登錄,如果登錄,顯示其登錄并退出;否則,顯示當前時間,并說明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 ~]#
寫一個腳本:
1) 顯示一個菜單給用戶:
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
2) 當用戶給定選項后顯示相應(yīng)的內(nèi)容;
擴展:
當用戶選擇完成,顯示相應(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
中斷當前這一次循環(huá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)容對大家有所幫助,如果還想學(xué)習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:bash腳本中如何使用while
標題URL:http://www.rwnh.cn/article32/jgpgpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網(wǎng)站導(dǎo)航、網(wǎng)站改版、網(wǎng)站設(shè)計、網(wǎng)站營銷、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)