开心六月综合激情婷婷|欧美精品成人动漫二区|国产中文字幕综合色|亚洲人在线成视频

    1. 
      
        <b id="zqfy3"><legend id="zqfy3"><fieldset id="zqfy3"></fieldset></legend></b>
          <ul id="zqfy3"></ul>
          <blockquote id="zqfy3"><strong id="zqfy3"><dfn id="zqfy3"></dfn></strong></blockquote>
          <blockquote id="zqfy3"><legend id="zqfy3"></legend></blockquote>
          打開APP
          userphoto
          未登錄

          開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

          開通VIP
          Shell 腳本的 if-else/case 用法

          if-then 語句

          if commandthen commands fi

          if-then 的語法和我們熟悉的其他高級(jí)編程語言有些差別,以C++為例,if 后面一般是條件判斷表達(dá)式,通過表達(dá)式返回 True or False,來決定運(yùn)行或者不運(yùn)行接下來的命令,而Shell 中的 if-then 不同,if 語句會(huì)執(zhí)行其后的命令,如果命令的退出碼為 '0', 則執(zhí)行 then 后的邏輯,如果退出碼非 '0',則不執(zhí)行。'fi' 表示 if-then 邏輯語句的結(jié)束。

          if-then-else 語句

          if commandthen 	commandselse commandsfi

          if-then-else 語句與if-then 相同,只是當(dāng)if 后免得command 返回碼為非 '0'時(shí),會(huì)執(zhí)行else 語句。

          if-then-elif-then 語句

          if command1then commandselif command2then commandselsethen commandsfi

          原理其實(shí)和if-then-else沒有區(qū)別,只是增加了邏輯分支的判斷匹配。

          test 命令

          test condition #test命令的格式

          if-then 系列的命令中,if語句不能直接判斷condition,而只能通過命令的返回碼來作為條件判斷依據(jù),這有些時(shí)候并不便利,而且和我們熟悉的其他高級(jí)語言中的 if-else 語法不同。test 命令可以幫助我們轉(zhuǎn)變shell 中的 if-then 結(jié)構(gòu)語法為我們熟悉的語法。標(biāo)準(zhǔn)的結(jié)構(gòu)是:

          if test conditionthen commandsfi

          bash shell中也提供了另一中方法來實(shí)現(xiàn) test 相同的結(jié)果, 將condition 用方括號(hào)[]括起來

          if [ condition ] #方括號(hào)和condition之間要有空格then  commandsfi

          數(shù)值比較

          結(jié)合上面的內(nèi)容,舉一個(gè)例子說明:

          輸出結(jié)果為:

          the value of 10 is greater than value of 5value of 5 is equal to 5

          注:bash shell中只能比較整數(shù)。

          字符串比較

          因?yàn)樽址容^中,直接用了數(shù)學(xué)符號(hào),而bash中會(huì)將大于號(hào)和小于號(hào)理解為重定向符號(hào)(之前的文章中有介紹重定向的含義和用法),所以做字符串比較是需要對(duì)大于號(hào)和小于號(hào)做轉(zhuǎn)義,轉(zhuǎn)義方式為在 '<' or '>' 之前加 '\'. 直接上例子

          輸出結(jié)果為:'hello is less than world'

          文件比較

          復(fù)合條件測(cè)試

          [ condition1 ] && [ condition2 ] [ condition1 ] | | [ condition2 ]

          條件判斷高級(jí)用法

          1.使用雙圓括號(hào), 且雙圓括號(hào)內(nèi)的 '<' or '>' 號(hào)不需要轉(zhuǎn)義。

          (( expression ))

          雙圓括號(hào)可以使用的表達(dá)式更多,如下

          2.使用雙方括號(hào), 注:并不是所有的bash都支持雙方括號(hào)語法。

          [[ expression ]]

          case 命令

          shell 中的 case 命令和其他高級(jí)語言的用法是一樣的,都是為了解決 if-then-elif 太多的情況,可以是代碼更清晰。case 語句的格式為

          case variable in pattern1 | pattern2) command1;;pattern3) command2;;*) default commands;;esac

          直接上例子:

          本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
          打開APP,閱讀全文并永久保存 查看更多類似文章
          猜你喜歡
          類似文章
          shell入門基礎(chǔ)(4)
          學(xué)習(xí)Linux課程第五天
          sql 語句中的語法 case when ... then ... else ...end
          shell if--七個(gè)部分
          shell 十三問之第十二問:你要 if 還是 case
          Shell 流程控制
          更多類似文章 >>
          生活服務(wù)
          分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
          綁定賬號(hào)成功
          后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
          如果VIP功能使用有故障,
          可點(diǎn)擊這里聯(lián)系客服!

          聯(lián)系客服