關於shell script的問題 [論壇 - Ubuntu 程式設計]
正在瀏覽:
1 名遊客
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 36
HP : 178 / 894
![]() |
[回到索引]
================================================================================ 接下來再回到「條件判斷」的議題。 ================================================================================ 可以執行下面指令,觀看「test」說明
顯示 test: test [expr] Evaluate conditional expression. Exits with a status of 0 (true) or 1 (false) depending on the evaluation of EXPR. Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. There are string operators and numeric comparison operators as well. The behavior of test depends on the number of arguments. Read the bash manual page for the complete specification. ...略... 還是離不開「Exit Status」。 ================================================================================ 先執行下面指令,
沒有顯示任何結果,直接出現下一個提示字元 接著馬上執行下面指令
顯示
這個「0」就是「Exit Status (Exit Code)」。 在「shell」,「0」代表著「true」。 ================================================================================ 先執行下面指令,
沒有顯示任何結果,直接出現下一個提示字元 接著馬上執行下面指令
顯示
這個「1」就是「Exit Status (Exit Code)」。 在「shell」,非「0」代表著「false」。 ================================================================================ 接著搭配「if」來使用
執行 if test 'aaa' = 'aaa'; then echo 'yes' fi 會顯示「yes」。 ================================================================================ 執行 if test 'aaa' = 'bbb'; then echo 'yes' fi 不會顯示「yes」。 ================================================================================ 上面的「test 'aaa' = 'aaa';」和「test 'aaa' = 'bbb';」就是「if COMMANDS;」中的「COMMANDS;」。 ================================================================================ 而「'aaa' = 'aaa'」和「'aaa' = 'bbb'」就是「expression」。 然而其他更多的「expression」可以執行下面指令,來觀看說明。 $ help test 或是執行 $ man test ================================================================================ 接下來執行
顯示 [: [ arg... ] Evaluate conditional expression. This is a synonym for the "test" builtin, but the last argument must be a literal `]', to match the opening `['. ================================================================================ 所以我們可以把「test」用「[」來取代,但最後要多加上「]」。 所以上面的「test 'aaa' = 'aaa';」就變成了「[ 'aaa' = 'aaa' ];」。 搭配「if」,就變成了 if [ 'aaa' = 'aaa' ]; then echo 'yes'; fi 會顯示「yes」。 若沒搭配「if」,直接執行「$ [ 'aaa' = 'aaa' ]」,然後接著執行「$ echo $?」,會顯示「0」。 若是執行「$ [ 'aaa' = 'aaa'」,後面少了「]」,就會顯示「bash: [: missing `]'」。 ================================================================================ 所以上面的「test 'aaa' = 'bbb';」就變成了「[ 'aaa' = 'bbb' ];」。 搭配「if」,就變成了 if [ 'aaa' = 'bbb' ]; then echo 'yes'; fi 沒有顯示任何結果,直接出現下一個提示字元。 若沒搭配「if」,直接執行「$ [ 'aaa' = 'bbb' ]」,然後接著執行「$ echo $?」,會顯示「1」。 若是執行「$ [ 'aaa' = 'bbb'」,後面少了「]」,就會顯示「bash: [: missing `]'」。 ================================================================================ 還有一個「[[」,這個在「#3」有使用,在「判斷數字」那,有用到「=~」。 執行下面指令,觀看說明
或是也可以執行,觀看說明
顯示 [[ ... ]]: [[ expression ]] Execute conditional command. Returns a status of 0 or 1 depending on the evaluation of the conditional expression EXPRESSION. Expressions are composed of the same primaries used by the `test' builtin, and may be combined using the following operators: ( EXPRESSION ) Returns the value of EXPRESSION ! EXPRESSION True if EXPRESSION is false; else false EXPR1 && EXPR2 True if both EXPR1 and EXPR2 are true; else false EXPR1 || EXPR2 True if either EXPR1 or EXPR2 is true; else false When the `==' and `!=' operators are used, the string to the right of the operator is used as a pattern and pattern matching is performed. When the `=~' operator is used, the string to the right of the operator is matched as a regular expression. The && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to determine the expression's value. Exit Status: 0 or 1 depending on value of EXPRESSION. ================================================================================ ## 關於「!」 先執行
接著再執行
顯示
================================================================================ 先執行
接著再執行
顯示
================================================================================ 先執行
接著再執行
顯示
================================================================================ 先執行
接著再執行
顯示
================================================================================ 先執行
接著再執行
顯示
================================================================================ 先執行
接著再執行
顯示
================================================================================ 先執行
接著再執行
顯示
================================================================================ 先執行
接著再執行
顯示
================================================================================ 執行 if [ ! "aaa" = "aaa" ]; then echo 'no' fi 沒有顯示任何結果,直接跳到下一個提示字元 ================================================================================ 執行 if [ ! "aaa" = "bbb" ]; then echo 'no' fi 顯示
================================================================================ 上面提到的概念,一樣可以套用在「while」和「until」上 ================================================================================ [回到索引]
2017/11/6 0:27
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 36
HP : 178 / 894
![]() |
[回到索引]
================================================================================ 接續著「上一篇」,提到的「if」搭配「test」使用 這一篇要將介紹「while」搭配「test」使用。 ================================================================================ 先來找尋「while」的語法, 執行下面指令
顯示
拆成多行 while COMMANDS; do COMMANDS; done ================================================================================ ## 使用「test」 執行 while test 'aaa' = 'aaa'; do echo 'yes' done 就會跑無窮迴圈,一直顯示「yes」。 關於「test 'aaa' = 'aaa';」這一段, 請查詢「help test」,或是複習上面「#11」提到的概念。 ================================================================================ ## 使用「[」 執行 while [ 'aaa' = 'aaa' ]; do echo 'yes' done 一樣會跑無窮迴圈,一直顯示「yes」。 關於「[ 'aaa' = 'aaa' ];」這一段, 請查詢「help test」和「help [」,或是複習上面「#11」提到的概念。 ================================================================================ ## 只跑三次 (使用「test」) 執行 times=0 while test $times -lt 3; do times=$(expr $times + 1); echo 'yes' done 顯示
只跑三次就停止了。 關於「test $times -lt 3;」這一段, 請查詢「help test」,或是複習上面「#11」提到的概念。 關於「expr $times + 1」,請查詢「man expr」,您可以在「Terminal」執行「expr 2 + 3」會顯示「5」。 關於「$(expr $times + 1)」,請查詢「man bash」,使用「Command Substitution」來找尋,會找到下面一段說明。 Command Substitution Command substitution allows the output of a command to replace the command name. There are two forms: $(command) or `command` 然後您可以複習「#1」,我有提到一個要特別注意的地方。 所以上面「times=$(expr $times + 1);」, 您也可以先在「Terminal」上練習,先執行「times=$(expr 5 + 1)」,然後在執行「echo $times」,就會顯示「6」。 ================================================================================ ## 只跑三次 (使用「[」) 執行 times=0 while [ $times -lt 3 ]; do times=$(expr $times + 1); echo 'yes' done 顯示
只跑三次就停止了。 ================================================================================ ## 只跑三次 (使用「break」) 執行 times=0 while true; do times=$(expr $times + 1); if [ $times -gt 3 ]; then break; fi echo 'yes' done 顯示
只跑三次就停止了。 關於「break」的用法,可以查詢 $ help break ================================================================================ ## 使用「break」和「continue」 執行 times=0 while true; do times=$(expr $times + 1); if [ $times -eq 5 ]; then continue; fi if [ $times -gt 10 ]; then break; fi echo 'times:' $times done 顯示
只跑十次就停止了。 這個關鍵在於 if [ $times -gt 10 ]; then break; fi 然而第五次,並沒有顯示「times: 5」 這個關鍵在於 if [ $times -eq 5 ]; then continue; fi 關於「break」的用法,可以查詢 $ help break 關於「continue」的用法,可以查詢 $ help continue ================================================================================ 關於「until」搭配「test」的用法,就請您理解這篇,您接著來「觸類旁通」了。 ================================================================================ ## 變化寫法 範本 #!/usr/bin/env bash times=0 while test $times -lt 3; do times=$(expr $times + 1) echo "times=$times" done 接著改成 #!/usr/bin/env bash is_next () { test $times -lt 3 } times=0 while is_next; do times=$(expr $times + 1) echo "times=$times" done 接著改成 #!/usr/bin/env bash is_next () { times=$(expr $times + 1) test $times -le 3 } times=0 while is_next; do echo "times=$times" done 接著改成 #!/usr/bin/env bash do_something () { times=$(expr $times + 1) echo "times=$times" test $times -lt 3 } times=0 while do_something; do :; done 接著改成 #!/usr/bin/env bash do_something () { times=$(expr $times + 1) echo "times=$times" test $times -lt 3 } times=0 while do_something; do :; done 相關文件 $ help : $ help function $ help return ================================================================================ 以上提供參考 報告完畢 ![]() ================================================================================ [回到索引]
2017/11/6 0:33
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 36
HP : 178 / 894
![]() |
## 索引
* 「#7」 (關於「Exit Status」) * 「#8」 (關於「if」和「Exit Status」) * 「#9」 (關於「while」和「Exit Status」) * 「#10」 (關於「until」和「Exit Status」) * 「#11」 (關於「test」和「Exit Status」以及「if」搭配「test」) * 「#12」 (關於「while」搭配「test」) * 「#3」 (應用案例) 這個索引,也紀錄在「[索引]如何執行指令」,在最下方的「## Shell Script 簡易入門」。 ================================================================================ ## 文件 ### bash * $ help * $ man bash * $ info bash * $ man bash-builtins * $ ls /usr/share/doc/bash/ -1 ### sh * $ man sh * $ man 1posix sh ## 文件 Package * $ sudo apt-get install bash-doc * $ sudo apt-get install manpages-posix 註: * 要執行「info bash」,請安裝「bash-doc」。 * 要執行「man 1posix sh」,請安裝「manpages-posix」。
2017/11/6 0:52
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員二級
![]() ![]() 註冊日期:
2011/10/7 0:22 所屬群組:
已註冊使用者 等級: 7
HP : 0 / 157
![]() |
密碼的部份建議先經過雜湊或類似gpg程式之類的不對稱加密演算之後,再另外儲存或再寫在程式裡(不推跟程式寫在一起)。
簡單的雜湊工具可用: sha512sum <<< 'hi' | awk '{print $1}' 獲得難以反推回原密碼的結果。 印象中,前一陣子有流行講解系統儲存密碼的方式。 洪朝貴教授也有寫過一篇不錯的文章: 單向雜湊函數的實用意義 & 如何手動修改密碼檔 如果,這是發問者的一份作業,那將來會有很大的機率從事相關產業,大眾未來的個資安全,就靠您現在在學習上的努力了!
2017/11/6 22:55
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 36
HP : 178 / 894
![]() |
Losepacific 寫到: 感恩提醒! 寫文的時候,我倒是沒考慮到這一點, 密碼學這方面我真的是不專業,差點誤人子弟了,Orz... 已經在「#3」的最下方附註了。 再次感謝! ![]()
2017/11/6 23:41
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員一級
![]() ![]() 註冊日期:
2017/11/3 18:11 所屬群組:
已註冊使用者 等級: 1
HP : 0 / 5
![]() |
@samwhelp
謝謝前輩那麼精細的講解!! 正在努力了解迴圈當中!! 謝謝哦~~
2017/11/7 17:18
|
||||||||||
![]() |
您可以查看帖子.
您不可發帖.
您不可回覆.
您不可編輯自己的帖子.
您不可刪除自己的帖子.
您不可發起投票調查.
您不可在投票調查中投票.
您不可上傳附件.
您不可不經審核直接發帖.