迴圈中分號的功能 - Linux

By Hedwig
at 2017-02-11T19:05
at 2017-02-11T19:05
Table of Contents
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#ifthen
if [ 條件判斷式 ]; then
當條件判斷式成立時,可以進行的指令工作內容;
fi <==將 if 反過來寫,就成為 fi 啦!結束 if 之意!
if這邊有分號
while [ condition ] <==中括號內的狀態就是判斷式
do <==do 是迴圈的開始!
程式段落
done <==done 是迴圈的結束
for var in con1 con2 con3 ...
do
程式段
done
這個就沒有
至於case就是一定要兩個分號。
好!我之前HPC考試要交兩個script,當時想也沒想,就把大腸包小腸弄出三個
while和if的迴圈,寫在同一行,每個do或done前面都用分號隔開,順利執行。
後來回家整理一下覺很難讀,所以換成多行,這時才注意到鳥哥的範例中
只有if的then前面有分號,不過換成多行後我加上分號也沒有影響執行,
所以好奇分號的功能是甚麼?是必要的嗎?
w|tr -s ' '|cut -d ' ' -f1,4,5|\
while read U L T ;do
if [ `echo $T|cut -d ':' -f1` -ge 30 ] ;then
ps aux|grep "sshd: $U"|grep $L|tr -s ' '|cut -d ' ' -f2\
|while read KPID ;
do
kill -15 $KPID ;
done ;
fi ;
done
這是我整理後的格式,感覺分號只是當成斷行用的?如果"then","do","fi"這類的
寫在不同行就可以不用寫分號,這樣對嗎?
--
"Don't hate the player. Hate the game."
當看到哪個名人賺了大錢,領到高額補助或利息,不要去指責他,
而是要指責那個制度、創造那個制度的人,以及默許那個制度的人。
不然你認為一個人不領18%或是一個人不炒房價,就會改變整體環境嗎?
還不如改變【修改制度的人】,才能從根源改變一切。
--
if [ 條件判斷式 ]; then
當條件判斷式成立時,可以進行的指令工作內容;
fi <==將 if 反過來寫,就成為 fi 啦!結束 if 之意!
if這邊有分號
while [ condition ] <==中括號內的狀態就是判斷式
do <==do 是迴圈的開始!
程式段落
done <==done 是迴圈的結束
for var in con1 con2 con3 ...
do
程式段
done
這個就沒有
至於case就是一定要兩個分號。
好!我之前HPC考試要交兩個script,當時想也沒想,就把大腸包小腸弄出三個
while和if的迴圈,寫在同一行,每個do或done前面都用分號隔開,順利執行。
後來回家整理一下覺很難讀,所以換成多行,這時才注意到鳥哥的範例中
只有if的then前面有分號,不過換成多行後我加上分號也沒有影響執行,
所以好奇分號的功能是甚麼?是必要的嗎?
w|tr -s ' '|cut -d ' ' -f1,4,5|\
while read U L T ;do
if [ `echo $T|cut -d ':' -f1` -ge 30 ] ;then
ps aux|grep "sshd: $U"|grep $L|tr -s ' '|cut -d ' ' -f2\
|while read KPID ;
do
kill -15 $KPID ;
done ;
fi ;
done
這是我整理後的格式,感覺分號只是當成斷行用的?如果"then","do","fi"這類的
寫在不同行就可以不用寫分號,這樣對嗎?
--
"Don't hate the player. Hate the game."
當看到哪個名人賺了大錢,領到高額補助或利息,不要去指責他,
而是要指責那個制度、創造那個制度的人,以及默許那個制度的人。
不然你認為一個人不領18%或是一個人不炒房價,就會改變整體環境嗎?
還不如改變【修改制度的人】,才能從根源改變一切。
--
Tags:
Linux
All Comments

By Isabella
at 2017-02-14T20:20
at 2017-02-14T20:20

By Xanthe
at 2017-02-17T12:30
at 2017-02-17T12:30

By Robert
at 2017-02-21T21:12
at 2017-02-21T21:12

By Quintina
at 2017-02-22T13:47
at 2017-02-22T13:47

By Genevieve
at 2017-02-27T02:52
at 2017-02-27T02:52

By Edwina
at 2017-02-28T02:43
at 2017-02-28T02:43

By Xanthe
at 2017-03-04T01:31
at 2017-03-04T01:31

By Thomas
at 2017-03-04T08:13
at 2017-03-04T08:13

By Elvira
at 2017-03-04T11:11
at 2017-03-04T11:11

By Leila
at 2017-03-04T14:55
at 2017-03-04T14:55

By Andy
at 2017-03-09T04:01
at 2017-03-09T04:01
Related Posts
物聯網,你可能會需要考量的事 by StarNight

By Isla
at 2017-02-10T10:19
at 2017-02-10T10:19
關於安裝問題(已解決)

By Liam
at 2017-02-09T20:25
at 2017-02-09T20:25
shell script 抓關鍵字問題

By Christine
at 2017-02-09T15:49
at 2017-02-09T15:49
數值排列

By Belly
at 2017-02-09T15:02
at 2017-02-09T15:02
新手請教

By Kama
at 2017-02-09T13:39
at 2017-02-09T13:39