日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Shell语法—— while 条件语句

發布時間:2023/12/9 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shell语法—— while 条件语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
while 循環語句

while 循環語句語法格式為:

while < 條件表達式 > do指令 done

有關腳本運行的相關用法

命令說明
sh file.sh &把 file.sh 腳本放到后臺運行(后臺運行腳本的常用方法)
ctl+c停止執行當前腳本或任務
ctl+z暫停執行當前腳本或任務
bg把當前腳本或任務放到后臺執行
fg把當前腳本或任務放到前臺執行。如果是多個腳本,需加編號,如: fg 2
jobs查看當前執行的腳本或任務
kill關閉執行的腳本任務

有關進程管理的 Linux 相關命令

命令說明
kill、killall、pkill殺掉進程
ps查看進程
pstree顯示進程狀態樹
top顯示進程
renice改變優先權
nohup用戶退出系統之后繼續工作
pgrep查找匹配條件的進程
strace跟蹤一個進程的系統調用情況
ltrace跟蹤進程調用庫函數的情況
小結

一. while 循環結構及相關語句綜合實踐

  • while 循環的特長是執行守護進程,以及實現我們希望循環持續執行不退出的應用,適合用于頻率小于 1min 的循環處理其他的 while 循環幾乎都可以被 for 循環及定時任務 crontab 功能代替
  • case 語句可以用 if 語句來替代,而在系統啟動腳本時傳入少量固定規則字符串的情況下,多用 case 語句,其他普通判斷多用 if 語句
  • 一句換場景,if 語句、for 語句最常用,其次是 while(守護進程)、case(服務啟動腳本)

  • 二. shell 腳本中各個語句的使用場景

  • 條件表達式,用于簡短的條件判斷及輸出(文件是否存在,字符串是否為空等)
  • if 取值判斷,多用于不同值數量較少的情況
  • for 最常用于正常的循環處理中
  • 案例一:
    猜數字
    隨機生成一個 100 以內的數字,讓用戶輸入數字來猜。游戲規則:輸入的必須是數字,猜錯時出現大小提示,才對時給出所用次數

    #!/bin/bash total=0 RETVAL=0 key=$((RANDOM%100)) echo "Let's play the guessing game!" echo "key:$key" panduan(){read -p "input num: " numexpr $num + 1 &>/dev/nullRETVAL=$?if [ $RETVAL -ne 0 ];thenecho "number number !!!"panduanfi }guess(){((total++))if [ $num -lt $key ];thenecho "It is too small"panduanelif [ $num -gt $key ];thenecho "It is too big"panduanelseecho "Guess right"echo "yi gong cai le $total ci"exit 0fi } main(){panduanwhile truedoguessdone } main $*

    案例二:
    手機充值并發送短信。這個案例并沒有實際用途,可以當做一個綜合練習玩玩的
    以下功能:

  • 充值,但重復充值金額不累計,充值后立即查詢余額無法正常使用
  • 發送短信,輸入發送的條數和短信內容,當金額不足時,可以選擇(y:充值,n:修改發送條數,o:返回菜單)
  • 查詢余額,只有當使用發送功能選擇(o:返回菜單)時,查詢才可以生常使用
  • 退出,結束當前腳本
  • #!/bin/bash RETVAL=0 balance=0 menu(){ cat << EOF1) Recharge2) Send information3) Query4) exit EOF read -p " please enter a number: " num case $num in1)echo "Welcome to chongzhi !"recharge;;2)echo "Welcome Check the balance !"send;;3)echo "Welcome to the query interface !"selects;;4)echo "Looking forward to your next arrival !"exit;;*)echo "Tips:{1|2|3|4}"menu esac }recharge(){read -p "please enter to RMB: " rmbexpr $rmb + 1 &>/dev/nullRETVAL=$?if [ $RETVAL -ne 0 ];thenecho "please input num"rechargefiecho "$rmbs Rechargs success"((rmbs=$rmb + $balance))menu }send(){while trueread -p "send several messages: " stripsexpr $strips + 1 &>/dev/nullRETVAL=$?if [ $RETVAL -ne 0 ];thenecho "please input num"sendfi((count=$strips * 15))doif [ $count -gt $rmbs ];thenecho "Insufficient amount !!!"read -p "Recharge(y|n|o) ?" orif [ $or = y -o $or = Y ];thenrechargeelif [ $or = n -o $or = N ];thensendelse menufielse read -p "Please input information content: " inputecho "sending:$input send success"((balance=$rmbs - $count))echo "Balance: $balance"fi((rmbs=$rmbs - $count))done }selects(){echo "Query:$balance"menu }menu $*

    案例三:
    使用 while 守護進程的方式監控網站,每隔 10s 檢測一次網站是否正常

    #!/bin/bash . /etc/init.d/functions count=0 url_list=( www.baidu.com blog.51cto.com www.taobao.com www.192.168.80.123.com )waits(){echo -n "3s run after"for((i=0;i<3;i++))doecho -n .sleep 1doneecho }url(){waitsfor((i=0; i<`echo ${#url_list[*]}`; i++))dowget -o /dev/null -T 3 --tries=1 --spider ${url_list[$i]}>/dev/null 2>&1if [ $? -eq 0 ];thenaction "${url_list[$i]}" /bin/trueelseaction "${url_list[$i]}" /bin/falsefidone((count++)) }main(){while truedourlecho "------count:$count------"sleep 10done } main while 循環按行讀文件

    方法一:

    cat 文件|while read line do命令 done

    方法二:

    while read line do命令 done<文件

    案例一:
    編寫一個 shell 腳本,實現 cat 命令的基本功能

    #!/bin/bath while read line doecho $line done</home/shell/fun.sh

    案例二:
    編寫 shell 腳本,根據短時間內的 PV 量封掉對應 ip,解決類 DoS 的功能

    #!/bin/bash count=0 while true do netstat -an|grep EST|awk -F '[ :]+' '{print $6}'|sort|uniq -c>ip.log exec<ip.log while read line dopv=`echo $line|awk '{print $1}'`ip=`echo $line|awk '{print $2}'`if [ $pv -gt 10 ]&&[ `iptables -nL|grep $ip|wc -l` -eq 0 ];theniptables -t filter -I INPUT -s $ip -j DROPecho "sun success"fi done((count++)) sleep 10 echo "---$count---" done

    轉載于:https://blog.51cto.com/12384628/2297292

    總結

    以上是生活随笔為你收集整理的Shell语法—— while 条件语句的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。