--- title: 【Shell Script】if ... else 條件判斷式 date: 2019-06-14 is_modified: false disqus: cynthiahackmd categories: - "程式設計 › 程式語言與架構" tags: - "Shell Script" --- {%hackmd @CynthiaChuang/Github-Page-Theme %} <br> Shell Script 的條件宣告是用 **[]** ,if 條件後面需接 **then** ,block 結束後接 **fi** <!--more--> <br> 寫法大概長這樣 ```bash= if [ 條件 ]; then # do something elif [ 條件 ]; then # do something else # do something fi ``` <br><br> 是說在寫條件式的時候稍微卡了一下,習慣性比較時直接用了 `==` ```bash= name="Cynthia" if [ "$name" == "Cynthia" ]; then # do something fi ``` 不過執行時一直跳出 **[: Cynthia: unexpected operator**,稍微 de 了一下 bug,後來才想到,我測試時是使用 sh run ,而 `==` 是 bash 的語法。所以要嘛改用 bash run,不然就把 `==` 改成 `=` 。 ## 參考資料 1. [第十二章、學習 Shell Scripts|鳥哥的 Linux 私房菜](http://linux.vbird.org/linux_basic/0340bashshell-scripts.php) <br><br> > **本文作者**: 辛西亞.Cynthia > **本文連結**: [辛西亞的技能樹](https://cynthiachuang.github.io/Shell-Script-If-Elif-Statement) / [hackmd 版本](https://hackmd.io/@CynthiaChuang/Shell-Script-If-Elif-Statement) > **版權聲明**: 部落格中所有文章,均採用 [姓名標示-非商業性-相同方式分享 4.0 國際](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en) (CC BY-NC-SA 4.0) 許可協議。轉載請標明作者、連結與出處!
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up