SHELL 學習之路(6)迴圈和判斷

2021-09-05 09:11:59 字數 1549 閱讀 1876

######for######

for 條件

do動作

done

示例:倒計時

#!/bin/bash

read -p 「please input two numbers:」 hour min sec

time=((((

((hour*3600+min

∗60

+min*60+

min∗60

+sec))

for ((a=$time;a>0;a–))

doecho -n "time ((((

((a/3600)) h ((((

((a%3600/60)) m ((((

((a%3600%60)) s "

echo -ne 「\r」

sleep 1

done

######while######

while 條件

do動作

done

示例:建立westos(1-20),並設定其密碼為123456

#!/bin/bash

prefix=「westos」

i=1while [ $i -le 20 ]

douserdel -r pre

fixpr

efix

i &> /dev/null

echo 「123456」 | passwd --stdin pre

fixpr

efix

i &> /dev/null

((i++))

done

######if-else######

if 條件;then

動作1elif

動作2elif

動作3else

動作4fi

檢視使用者是否存在:

#!/bin/bash

user=student

if grep $user /etc/passwd;then

echo "the files for user use

rare

:"ls

−a/h

ome/

user are:" ls -a /home/

userar

e:"l

s−a/

home

/user

else

echo 「$user not exist!」

fi#######多重迴圈############

示例:列印99乘法表

#11=1

#21=2 22=4

#31=3 32=6 3*3=9

for i in $(seq 9)

dofor j in $(seq i)d

oech

o−ne

"i) do echo -ne "

i)doec

ho−n

e"i*j =j=

j=((i ∗i*

i∗j))\t"

done

echo -e 「\n」

done

shell 死迴圈if判斷 shell 死迴圈

例1 執行指令碼後會自動載入firefox瀏覽器,並開啟指定網頁。如果使用者關閉firefox,指令碼會再次自動重新開啟firefox。如需結束迴圈,中止t2.sh程序即可。注意不要同時執行兩個t1.sh指令碼,否則 若事先不知道指令碼名,也可以通過類似於 pstree grep firefox 的...

shell(顯示 條件判斷 迴圈)

目錄 shell 顯示 echo printf 條件判斷 if case 迴圈 forwhile bin bash 顯示轉義字元 echo it s a test 顯示變數 read 從標準輸入中讀取一行,相當於scanf read name echo name is name 顯示換行 echo ...

shell 死迴圈if判斷 shell實現死迴圈

ios網路第二天 09 多值引數 import hmviewcontroller.h import mbprogresshud mj.h inte ce dfs hdoj 2614 beat 題目傳送門 題意 處理完i問題後去處理j問題,要滿足a i j a j k 問最多能有多少問題可以解決 df...