關於shell script的問題 [論壇 - Ubuntu 程式設計]
正在瀏覽:
1 名遊客
關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員一級
![]() ![]() 註冊日期:
2017/11/3 18:11 所屬群組:
已註冊使用者 等級: 1
HP : 0 / 5
![]() |
1.請問要如何以while為主寫一個累加程式,使用者可以一直輸入數字進行累加,直到使用者輸入END或是非數字文字後,顯示累加及平均數。
2.以until寫一密碼驗證程序,密碼只能錯三次。 請問各位神人,這兩個該如何寫呢.....
2017/11/3 18:17
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
管理員
![]() ![]() 註冊日期:
2012/1/14 18:41 所屬群組:
討論區管理群 等級: 20
HP : 0 / 480
![]() |
AWEI 寫到: 看題目很像是作業,你自己有想過嗎?
2017/11/3 22:06
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 37
HP : 0 / 901
![]() |
[回到索引]
================================================================================ 幻影火 寫到:AWEI 寫到: 先給您一些參考連結,讓您有個探索的起點。 * 第十二章、學習 Shell Scripts * bash-handbook 其實真的要找,在網路上可以找到很多參考資料。 不過太多資料有可能會失焦,所以就給您我比較記得的地方。 :p ## 相關索引 * [索引]如何執行指令 * bash-handbook * 阿旺的 Linux 開竅手冊 ================================================================================ 而回歸到原點,最重要可以參考的「Manual」是 $ help $ man bash $ man bash-builtins $ man sh ## 如何觀看文件,請參考 * 鳥哥的 Linux 私房菜 / 第四章、首次登入與線上求助 / 4.3 Linux系統的線上求助man page與info page ================================================================================ 因為目前網路上的資料很豐富,所以「Google」也很好用, 您可以使用「bash 累加」和「bash 判斷數字」當關鍵字, 從中找到的參考文章,大概就可以把答案拼湊起來。 我剛就是這麼查的 :p ================================================================================ 當然我有些基礎的概念。 所以我建議您要著重學習的,就是把「條件判斷」和「迴圈」的概念學會,您大概就能舉一反三了。 以各個程式語言來說, 不管是「if else」還是「switch case」等等,對我來說,概念上會被我歸類在「條件判斷」的範疇,只是語法的不同。 不管是「while」,「until」,「for」,「foreach」等等,對我來說,概念上會被我歸類在「迴圈」的範疇,只是語法的不同。 ================================================================================ 雖然我個人一開始也覺得不該直接給您答案, 不過後來我考量了一下,還是給您答案參考吧,不過我會先引導您去找相關的文件來閱讀。 也許您能從中找到探索的起點。 剛好這兩天看到一句已經被我遺忘的句子, 「學而不思則罔 思而不學則殆」, 這個句子,我用來對自己省思,也提供給您參考。 ![]() ================================================================================ 先聲明,我不是什麼神人。 我剛也是使用「bash 累加」和「bash 判斷數字」當關鍵字查詢,拼湊出下面的答案。 ================================================================================ 關於 AWEI 寫到: ### 可以參考的文件 (bash版) $ help while 顯示
$ help if 顯示
$ Wikipedia / Shebang (Unix) $ help test $ man test $ help [ $ man [ $ help [[ $ help break $ help continue $ help read $ man expr $ 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` ### 參考答案 (bash版) #!/usr/bin/env bash sum=0 while read -p 'Please Enter Number:' num; do num=$(echo "$num" | sed -e 's/\ //g' ) if [[ "$num" =~ ^[1-9]+$ ]]; then echo 'Plus:' $num; sum=$(expr $sum + $num); ## Command Substitution ## sum=`expr $sum + $num`; ## Command Substitution else break; fi done echo Sum: $sum ### 參考答案 (php版) #!/usr/bin/env php <?php $sum=0; while (true) { $num = readline('Please Enter Number: '); if (is_numeric($num)) { $sum = $sum + $num; echo 'Plus: ' . $num; echo PHP_EOL; } else { break; } } echo 'Sum: ' . $sum; echo PHP_EOL; ### 可以參考的文件 (php版) * http://php.net/manual/en/control-structures.while.php * http://php.net/manual/en/control-structures.break.php * http://php.net/manual/en/control-structures.continue.php * http://php.net/manual/en/function.is-numeric.php * http://php.net/manual/en/function.readline.php * http://php.net/manual/en/function.echo.php * http://php.net/manual/en/reserved.constants.php#constant.php-eol * http://php.net/manual/en/language.types.string.php * http://php.net/manual/en/language.types.integer.php ================================================================================ 關於 AWEI 寫到: ### 可以參考的文件 (bash版) $ help until 顯示
$ help while 顯示
$ help if 顯示
$ Wikipedia / Shebang (Unix) $ help test $ man test $ help [ $ man [ $ help [[ $ help break $ help continue $ help read $ help exit $ help true $ help false $ man expr $ 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` ### 參考答案 (bash until 版) #!/usr/bin/env bash times=0 password="hi" until [ $times -ge 3 ] ; do read -p 'Please Enter Password:' pass; times=$(expr $times + 1); if [ "$pass" == "$password" ]; then echo 'You Pass'; exit 0; break; else echo "$times incorrect password attempts"; echo; fi done echo 'Over max number of times!'; exit 1; ### 參考答案 (bash while 版) #!/usr/bin/env bash times=0 password="hi" while true ; do if [ $times -ge 3 ] ; then echo 'Over max number of times!' exit 1; ## break; fi read -p 'Please Enter Password:' pass; times=$(expr $times + 1) if [ "$pass" == "$password" ]; then echo 'You Pass'; exit 0; ## break; else echo "$times incorrect password attempts" echo fi done ### 參考答案 (php版) #!/usr/bin/env php <?php $times=0; $password='hi'; while (true) { $times += 1; if ($times > 3) { echo 'Over max number of times!'; echo PHP_EOL; exit(1); //break; } $pass = readline('Please Enter Password: '); if (trim($pass) === trim($password)) { echo 'You Pass'; echo PHP_EOL; exit(0); //break; } else { echo "$times incorrect password attempts"; echo PHP_EOL; echo PHP_EOL; //continue; } } ### 可以參考的文件 (php版) * http://php.net/manual/en/control-structures.while.php * http://php.net/manual/en/control-structures.break.php * http://php.net/manual/en/control-structures.continue.php * http://php.net/manual/en/function.is-numeric.php * http://php.net/manual/en/function.readline.php * http://php.net/manual/en/function.echo.php * http://php.net/manual/en/reserved.constants.php#constant.php-eol * http://php.net/manual/en/language.types.string.php * http://php.net/manual/en/language.types.integer.php * http://php.net/manual/en/function.exit.php ================================================================================ 以上提供參考 報告完畢 ![]() ================================================================================ 後記(2017-11-06): 關於密碼的處理,「#14」,「Losepacific」有提醒和補充說明,太感謝了。 實做的部份,就留給您當練習了,懂了基本技巧,跨過一個門檻, 要來舉一反三,就比較輕鬆一點了。 要了解「比對編碼後的密碼」實作的模型(原理),可以參考下面兩個網址裡面的舉例。 了解了原理,就有機會舉一反三,修改成自己的程式。 * http://php.net/manual/en/function.md5.php#refsect1-function.md5-examples * http://php.net/manual/en/function.sha1.php#refsect1-function.sha1-examples ## 其他更多相關的參考文件 * https://blog.longwin.com.tw/2015/10/php-sha256-sha512-hash-algorithm-2015/ * http://php.net/manual/en/function.hash.php * man sha512sum * man sha512 下面是之前寫的文章,裡面有列一些相關的參考連結 * https://samwhelp.github.io/book-ubuntu-qna/read/case/release-cdimage/ * https://samwhelp.github.io/book-ubuntu-qna/read/case/release-cdimage/1604.html ## 相關的工具 執行
顯示
表示「/usr/bin/sha512sum」來自「coreutils」這個套件。 所以執行下面指令,用「sum」當關鍵字,過濾出檔案列表。
顯示
## Manpage * man b2sum * man cksum * man md5sum * man md5sum.textutils * man sha1sum * man sha224sum * man sha256sum * man sha384sum * man sha512sum * man sum ![]() ================================================================================ [回到索引]
2017/11/4 3:42
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2010/9/26 16:05 所屬群組:
已註冊使用者 等級: 27
HP : 0 / 660
![]() |
AWEI 寫到: 作業自已寫
2017/11/4 13:54
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員一級
![]() ![]() 註冊日期:
2017/11/3 18:11 所屬群組:
已註冊使用者 等級: 1
HP : 0 / 5
![]() |
有嘗試著找資料...似乎方向錯誤不太懂..
2017/11/5 17:10
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員一級
![]() ![]() 註冊日期:
2017/11/3 18:11 所屬群組:
已註冊使用者 等級: 1
HP : 0 / 5
![]() |
謝謝你哦~原來關鍵字要怎麼查找...
謝謝,繼續加油去~
2017/11/5 17:16
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 37
HP : 0 / 901
![]() |
[回到索引]
================================================================================ 剛剛突然想到,之前少寫了一些,要提醒您該注意的地方。 ================================================================================ ## Exit Status 我認為您第一個學習的應該要先了解「Exit Status (Exit Code)」的概念。 * http://manpages.ubuntu.com/manpages/xenial/en/man1/bash.1.html#contenttoc23 * https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html * http://tldp.org/LDP/abs/html/exit-status.html * https://github.com/denysdovhan/bash-handbook#exit-codes 這個會跟「條件判斷」有關。 ================================================================================ 您可以在「Terminal」上執行下面指令
沒有顯示任何結果,直接出現另一個提示字元, 接著馬上執行
顯示
這個「0」就是「Exit Status (Exit Code)」。 在「shell」,「0」代表著「true」。 關於「true」這個指令,可以執行「help true」觀看說明。
顯示 true: true Return a successful result. Exit Status: Always succeeds. ================================================================================ 您可以在「Terminal」上執行下面指令
沒有顯示任何結果,直接出現另一個提示字元, 接著馬上執行
顯示
這個「1」就是「Exit Status (Exit Code)」。 在「shell」,非「0」代表著「false」。 關於「true」這個指令,可以執行「help false」觀看說明。
顯示 false: false Return an unsuccessful result. Exit Status: Always fails. ================================================================================ 您可以在「Terminal」上執行下面指令 function do_something() { return 9; } do_something 記得在「do_something」按下「Enter」。 沒有顯示任何結果,直接出現另一個提示字元, 接著馬上執行
顯示
這個「9」就是「Exit Status (Exit Code)」。 在「shell」,非「0」代表著「false」。 關於「return」這個指令,可以執行「help return」觀看說明。
顯示 return: return [n] Return from a shell function. Causes a function or sourced script to exit with the return value specified by N. If N is omitted, the return status is that of the last command executed within the function or script. Exit Status: Returns N, or failure if the shell is not executing a function or script. ================================================================================ 您可以在「Terminal」上執行下面指令 function say_hello() { echo 'hello'; return 8; } say_hello 會顯示
然後出現另一個提示字元, 接著馬上執行
顯示
這個「8」就是「Exit Status (Exit Code)」。 ================================================================================ 您可以在「Terminal」上執行下面指令 function say_hello() { echo 'hello'; return 7; } MSG=$(say_hello) 沒有顯示任何結果,直接出現另一個提示字元, 接著馬上執行
顯示
這個「7」就是「Exit Status (Exit Code)」。 然後執行下面指令,
顯示
這裡就是要特別注意的地方。 用其他的程式語言一貫的思路來想, 會認為「function say_hello()」那裡是「return 7」, 所以推測「MSG」應該是「7」。 但在「Shell」,卻不是這樣的概念, 「MSG」反而是「hello」,也就是「echo 'hello';」那一段。 至於「return 7」,剛剛已經提了很多範例,每執行完一個指令後,就會放在「$?」。 ================================================================================ ## 從 c 程式,來看「Exit Status (Exit Code)」 執行下面指令,產生一個檔案「main.c」 cat > main.c <<EOF int main (int argc, char *argv[]) { return 6; } EOF 執行下面指令,觀看「main.c」這個檔案的內容。
顯示 int main (int argc, char *argv[]) { return 6; } 執行下面指令,編譯
就會產生一個執行檔「app」。 然後執行下面指令
沒有顯示任何結果,直接顯示下一個提示字元 接著馬上執行下面指令
顯示
這個「6」就是「Exit Status (Exit Code)」。 $ man 3 exit ================================================================================ ## 從 php 程式,來看「Exit Status (Exit Code)」 執行下面指令,產生一個檔案「main.php」 cat > main.php <<EOF #!/usr/bin/env php <?php exit(5); EOF 執行下面指令,觀看「main.php」這個檔案的內容。
顯示 #!/usr/bin/env php <?php exit(5); 執行下面指令,將「main.php」設為可執行
然後執行下面指令
沒有顯示任何結果,直接出現另一個提示字元 接著馬上執行下面指令
顯示
這個「5」就是「Exit Status (Exit Code)」。 * http://php.net/manual/en/function.exit.php ================================================================================ ## 從 python 程式,來看「Exit Status (Exit Code)」 執行下面指令,產生一個檔案「main.py」 cat > main.py <<EOF #!/usr/bin/env python3 exit(4) EOF 執行下面指令,觀看「main.py」這個檔案的內容。
顯示 #!/usr/bin/env python3 exit(4) 執行下面指令,將「main.py」設為可執行
然後執行下面指令
沒有顯示任何結果,直接出現另一個提示字元 接著馬上執行下面指令
顯示
這個「4」就是「Exit Status (Exit Code)」。 * https://docs.python.org/3.6/library/sys.html#sys.exit * https://docs.python.org/3.6/library/constants.html#exit ================================================================================ ## 從 shell script 程式,來看「Exit Status (Exit Code)」 執行下面指令,產生一個檔案「main.sh」 cat > main.sh <<EOF #!/usr/bin/env bash exit 3 EOF 執行下面指令,觀看「main.sh」這個檔案的內容。
顯示 #!/usr/bin/env bash exit 3 執行下面指令,將「main.sh」設為可執行
然後執行下面指令
沒有顯示任何結果,直接出現另一個提示字元 接著馬上執行下面指令
顯示
這個「3」就是「Exit Status (Exit Code)」。 關於「exit」這個指令,可以執行「help true」觀看說明。
顯示 exit: exit [n] Exit the shell. Exits the shell with a status of N. If N is omitted, the exit status is that of the last command executed. ================================================================================ 以上的觀念建立後, 接著來說明「條件判斷」。 ================================================================================ [回到索引]
2017/11/5 23:45
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 37
HP : 0 / 901
![]() |
[回到索引]
================================================================================ 接下來,先來初步介紹「if」的用法。 ================================================================================ ## 如何找尋「if」的語法 執行下面指令
顯示
我通常久了沒寫,就記不住語法,所以通常會先執行「help if」來喚起記憶。 然後我會將上面一行的寫法,拆成多行的寫法,變成如下 if COMMANDS; then COMMANDS; elif COMMANDS; then COMMANDS; else COMMANDS; fi 於是就可以依此樣版做修改,變成下面的範例。 ================================================================================ ## 範例一 if true; then echo 'yes' fi ## 範例二 if true; then echo 'yes' else echo 'no' fi ## 範例二 if true; then echo 'yes' elif false; then echo 'oh! my god!' else echo 'no' fi 上面的範例,都可以變回成一行, 例如:範例一,變回成一行如下 if true; then echo 'yes'; fi 還記得一開始「help if」的找到的格式嗎?
所以「COMMANDS;」就是「true;」,就是「echo 'yes';」。 至於這部份,可以執行「$ man bash」,然後從「SHELL GRAMMAR」那裡開始探索起。 ================================================================================ ## 關於 「if」 和「Exit Status」 先從剛剛的「範例一」來看 if true; then echo 'yes' fi 因為執行「true」這個「COMMAND」之後,得到「Exit Status」是「0」,所以代表著是「true」,就會執行到「echo 'yes'」 ================================================================================ ### 範例四 if false; then echo 'yes' fi 因為執行「false」這個「COMMAND」之後,得到「Exit Status」是「1」,所以代表著是「false」,所以就不會執行到「echo 'yes'」。 ### 範例五 function do_something() { return 0; } if do_something; then echo 'yes' fi 因為執行「do_something」這個「COMMAND(function)」之後,得到「Exit Status」是「0」,代表著是「true」, 所以就會執行到「echo 'yes'」。 ================================================================================ ### 範例六 function say_hello() { return 9; } if say_hello; then echo 'yes' fi 因為執行「say_hello」這個「COMMAND(function)」之後,得到「Exit Status」是「9」,代表著是「false」, 所以就不會執行到「echo 'yes'」。 ================================================================================ 執行 if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi Execute commands based on conditional. The `if COMMANDS' list is executed. If its exit status is zero, then the `then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list is executed in turn, and if its exit status is zero, the corresponding `then COMMANDS' list is executed and the if command completes. Otherwise, the `else COMMANDS' list is executed, if present. The exit status of the entire construct is the exit status of the last command executed, or zero if no condition tested true. Exit Status: Returns the status of the last command executed. 對照著看 ================================================================================ 以上就是,關於 「if」 和「Exit Status」的搭配用法, 這個概念也可以套用在「while」和「until」上。 ================================================================================ [回到索引]
2017/11/5 23:55
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 37
HP : 0 / 901
![]() |
[回到索引]
================================================================================ 接下來,先來初步介紹「while」的用法。 ================================================================================ ## 如何找尋「while」的語法 執行下面指令
顯示
拆成多行 while COMMANDS; do COMMANDS; done ================================================================================ ## 關於 「while」 和「Exit Status」 ### 範例一 while true; do echo 'yes' done 跟上一篇,關於 「if」 和「Exit Status」搭配的概念是一樣的。 因為執行「true」這個「COMMAND」之後,得到「Exit Status」是「0」,代表著是「true」, 因為是「while」,所以會執行到「echo 'yes'」。 接著會再執行一次「true」這個「COMMAND」,得到「Exit Status」是「0」,代表著是「true」,所以會執行到「echo 'yes'」。 於是就是這樣不斷的循環,變成無窮迴圈。 ================================================================================ ### 範例二 while false; do echo 'yes' done 跟上一篇,關於 「if」 和「Exit Status」搭配的概念是一樣的。 因為執行「false」這個「COMMAND」之後,得到「Exit Status」是「1」,代表著是「false」, 因為是「while」,所以就不會執行到「echo 'yes'」。 而「while」也就到此中止了。 ================================================================================ 我上面的解釋可能不是很好,所以還是執行下面指令
顯示 while: while COMMANDS; do COMMANDS; done Execute commands as long as a test succeeds. Expand and execute COMMANDS as long as the final command in the `while' COMMANDS has an exit status of zero. Exit Status: Returns the status of the last command executed. 對照著看 ================================================================================ 接下來介紹「until」。 ================================================================================ [回到索引]
2017/11/6 0:02
|
||||||||||
![]() |
回覆: 關於shell script的問題 |
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
會員五級
![]() ![]() 註冊日期:
2012/4/22 10:50 所屬群組:
已註冊使用者 等級: 37
HP : 0 / 901
![]() |
[回到索引]
================================================================================ 接下來,先來初步介紹「until」的用法。 ================================================================================ ## 如何找尋「until」的語法 執行下面指令
顯示
拆成多行 until COMMANDS; do COMMANDS; done ================================================================================ ## 關於 「until」 和「Exit Status」 ### 範例一 until true; do echo 'yes' done 跟關於 「if」 和「Exit Status」搭配的概念是一樣的。 因為執行「true」這個「COMMAND」之後,得到「Exit Status」是「0」,代表著是「true」, 因為是「until」,所以就不會執行到「echo 'yes'」。 而「until」也就到此中止了。 這裡跟「while」相反。 ================================================================================ ### 範例二 until false; do echo 'yes' break; done 跟上一篇,關於 「if」 和「Exit Status」搭配的概念是一樣的。 因為執行「false」這個「COMMAND」之後,得到「Exit Status」是「1」,代表著是「false」, 因為是「until」,所以會執行到「echo 'yes'」。 接著會再執行一次「false」這個「COMMAND」,得到「Exit Status」是「1」,代表著是「false」,所以會執行到「echo 'yes'」。 於是就是這樣不斷的循環,變成無窮迴圈。 這裡跟「while」相反。 ================================================================================ 我上面的解釋可能不是很好,所以還是執行下面指令
顯示 until: until COMMANDS; do COMMANDS; done Execute commands as long as a test does not succeed. Expand and execute COMMANDS as long as the final command in the `until' COMMANDS has an exit status which is not zero. Exit Status: Returns the status of the last command executed. 對照著看 ================================================================================ 接下來再回到「條件判斷」的議題。 ================================================================================ [回到索引]
2017/11/6 0:05
|
||||||||||
![]() |
您可以查看帖子.
您不可發帖.
您不可回覆.
您不可編輯自己的帖子.
您不可刪除自己的帖子.
您不可發起投票調查.
您不可在投票調查中投票.
您不可上傳附件.
您不可不經審核直接發帖.