linux shell script程式之選擇結構簡介__linux

來源:互聯網
上載者:User

        很多人都是先學習C/C++語言, 然後再學習shell script的。 如果大家有一點點編程基礎, 那麼就很容易理解所謂的順序、選擇和迴圈。順序結構其實沒什麼好說的, 之前早就接觸過了, 在本文中, 我們來介紹一下選擇結構。 雖然簡單, 但還是要熟練正確地使用, 畢竟和C/C++的文法還是有一些出入的。 一不小心, 就容易出錯。


1. 先看個入門層級的:

[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashecho "enter your bank password"read passwdif [ $passwd = "3.1415" ]; thenecho yeselseecho nofi[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh enter your bank password1  no[taoge@localhost learn_shell]$ ./a.sh enter your bank password3.1415yes[taoge@localhost learn_shell]$ ./a.sh enter your bank password12  34./a.sh: line 5: [: too many argumentsno[taoge@localhost learn_shell]$

     上面的程式, 有如下幾點值得注意:

     (1) $passwd兩邊最好加上引號, 免得使用者輸入12 34的時候, 出現 12 34 = "3.1415"這樣的錯誤。

     (2) if後面必須有空格, 好噁心的規定啊。

     (3) [ ]裡面該加空格的地方一定要加, 否則會有錯誤。

     (4) 如果then和if處在同一行, 那麼then前面的那個分號絕對不能少。 當然, then可以放到下面獨立成行

     (5) 在比較相等的時候, 最好用==, 而不是=,  儘管後者也可以, 但是會讓很多c/c++程式猿感覺難以適應, 感覺不舒服。


      下面, 我來改一下, 使得上面程式更好:

[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashecho "enter your bank password"read passwdif [ "$passwd" == "3.1415" ]  # pay special attention to this line, my friends!thenecho yeselseecho nofi[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh enter your bank password1no[taoge@localhost learn_shell]$ ./a.sh enter your bank password3.1415yes[taoge@localhost learn_shell]$ ./a.sh enter your bank password12  34no[taoge@localhost learn_shell]$ 

      和C/C++一樣, linux shell script也會進行短路求值, 如下(我認為這個程式非常精妙):

[taoge@localhost learn_shell]$ cat a.sh#! /bin/bash#! /bin/bashecho "enter your bank password"read passwd[ "$passwd" == "3.1415" ] && echo yes[ "$passwd" == "3.1415" ] || echo no[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.shenter your bank password3.1415yes[taoge@localhost learn_shell]$ ./a.shenter your bank password3no[taoge@localhost learn_shell]$

    2. 看個稍微複雜一點點的, 其實也很簡單:

[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashecho "enter your login password"read passwdif [ "$passwd" == "yaoming" ]thenecho "hello yaoming, you are so tall"elif [ "$passwd" == liuxiang ]thenecho "hello liuxiang, you are so fast"elif [ "$passwd" == wangliqin ]thenecho "hello wangliqin, you play pingpong so well"else#no then in this lineecho "you do not belong here"fi[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh enter your login passwordyaominghello yaoming, you are so tall[taoge@localhost learn_shell]$ ./a.sh enter your login passwordliuxianghello liuxiang, you are so fast[taoge@localhost learn_shell]$ ./a.sh enter your login passwordwangliqinhello wangliqin, you play pingpong so well[taoge@localhost learn_shell]$ ./a.sh enter your login passwordzhangyiningyou do not belong here[taoge@localhost learn_shell]$

     3. 再看case:

[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashecho "enter your login password"read passwdcase "$passwd" in"yaoming")echo "hello yaoming, you are so tall";;"liuxiang")echo "hello liuxiang, you are so fast";;"wangliqin")echo "hello wangliqin, you play pingpong so well";;*)echo "you do not belong here";;esac[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh enter your login passwordyaominghello yaoming, you are so tall[taoge@localhost learn_shell]$ ./a.sh enter your login passwordliuxianghello liuxiang, you are so fast[taoge@localhost learn_shell]$ ./a.sh enter your login passwordwangliqinhello wangliqin, you play pingpong so well[taoge@localhost learn_shell]$ ./a.sh enter your login passwordzhangyiningyou do not belong here[taoge@localhost learn_shell]$

     需要注意: ;;就類似於C/C++語言中的break, 只不過, 在此處絕不可少, 否則運行shell的時候, 會出問題。

                         另外, 最後的*)位置相當重要, 必須放在最後, 因為linux shell script中的case是逐條前後逐條匹配的, 而C/C++中的default放置的位置則沒有限制, 放在最開始的位置也沒有任何關係。


     下面,我們來一起驗證一下, *)放在前面會導致邏輯錯誤, 如下:

[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashecho "enter your login password"read passwdcase "$passwd" in*)echo "you do not belong here";;"yaoming")echo "hello yaoming, you are so tall";;"liuxiang")echo "hello liuxiang, you are so fast";;"wangliqin")echo "hello wangliqin, you play pingpong so well";;esac[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh enter your login passwordyaomingyou do not belong here[taoge@localhost learn_shell]$

    

      4. test命令和[ ]命令

       先說明一下, test命令完全等價於[ ]命令, 可以任選其一, 當然, 每個人有不同的愛好, 我個人就比較喜歡[ ]命令, 下面, 我們來一起看一下:

[taoge@localhost learn_shell]$ cat a.sh #! /bin/bashecho "enter your login password"read passwdif test "$passwd" == "linux" ; thenecho yeselseecho nofi[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh enter your login passwordlinuxyes[taoge@localhost learn_shell]$ ./a.sh enter your login passwordLINUXno[taoge@localhost learn_shell]$
     我看著test就感覺挺彆扭的, 還是用[ ]吧。在用[ ]的時候一定要注意: [後面如果沒有空格, 那將是語法錯誤, 但是, 如果 ==左右沒有空格, 那就是 致命的邏輯錯誤, 此時if會永遠為真, 千千萬萬要注意啊。 linux shell script就是這樣, 到處都是蛇, 小心它咬你。


     最後, 我們以一個稍微複雜一點的程式來結束本文:

[taoge@localhost learn_shell]$ cat a.sh #! /bin/bash# compare stringname=""if [ "$name" == "" ]; thenecho log1elseecho log2fiif [ -z "$name" ]; thenecho log3elseecho log4finame="taoge"if [ -z "$name" ]; thenecho log5elseecho log6fiif [ -n "$name" ]; thenecho log7elseecho log8fiif [ "$name" == "taoge" ]; thenecho log9elseecho log10fi# compare numberif [ "$#" -eq 0 ]; thenecho log11elseecho log12fiif [ "$#" -lt 1 ]; thenecho log13elseecho log14fiif [ 1 -lt 2  -a  2 -lt 3 ]; thenecho log15elseecho log16fiif [ 1 -lt 2 ] && [ 2 -lt 3 ]; thenecho log17elseecho log18fi# test directory or filepwdif [ -e /home/taoge/Desktop/learn_shell ]; thenecho log19elseecho log20fiif [ -e /home/taoge/Desktop/learn_shell/test.html ]; thenecho log21elseecho log22fitouch /home/taoge/Desktop/learn_shell/test.htmlif [ -e /home/taoge/Desktop/learn_shell/test.html ]; thenecho log23elseecho log24fi[taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ [taoge@localhost learn_shell]$ ./a.sh log1log3log6log7log9log11log13log15log17/home/taoge/Desktop/learn_shelllog19log22log23[taoge@localhost learn_shell]$ 

      好吧, 先說到這裡。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.