在shell中使用while迴圈的例子

2021-04-22 17:06:13 字數 651 閱讀 6202

#!/bin/sh

#filename:2.sh

b=9e=15

tmpb=$b

while [ $tmpb -le $e ]

doecho $tmpb

#tmpb=`expr $tmpb + 1`  //ok

tmpb=$(expr $tmpb + 1)

done

執行這個指令碼後,輸出的結果為:910

1112

1314

15注意:   第9行的功能和第10行的功能是一樣的。

下面的例子是使用乙個迴圈,必須輸入乙個數字才能退出.

#!/bin/sh

#filename:b

while :

doecho -n "input a number:"

read val

expr $val + 1 > /dev/null 2>

res=$?

if [ "$val" = "" ] || [ "$res" != "0" ]; then

echo "what you input is not a number!"

else

echo "what you input is : $val"

break;

fidone

shell 程式設計之while迴圈和for迴圈

一 while迴圈 1.語法 一 while語句結構 條件為真時,執行迴圈體 while 條件 do 迴圈體 done 二 until語法結構 條件為假時,一直執行迴圈體 直到條件變為真 until 條件 do 迴圈體 done二 for迴圈 shell風格語法 for 變數名 in 取值列表 do...

shell指令碼中使用while存在隱患

20170315uat 批量,在給零售通知書新增民生logo的作業中 發現執行報錯,但是指令碼繼續執行,並且排程圖上沒有反應報錯情況。經排查,發現shell指令碼中在使用的while語句用法存在著隱患,具體情況如下 cat file while read line do if ne 0 then e...

Python列表細節分享 在 中使用for迴圈

用xpath爬取多個url時,可以通過在中使用for迴圈的方式將所需列表返回 如下 def parse index html etree lxml.html.etree e etree.html html all url e.xpath div class channel detail movie ...