<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## 中括號 / 雙中括號 區別 相同點 1. 兩個符號左右都要有空格分隔 2. 內部操作符與操作變量之間要有空格:如 `[[ "a" = "b" ]]` 3. 內部字符串或者${}變量盡量使用”” 雙引號擴住 單中括號 [ ] 1. 字符串比較中,> < 需要寫成 `\>` `\<` 進行轉義 1. [ ] 中可以使用 –a –o 進行邏輯運算 1. [ ] 是bash 內置命令 雙中括號 1. 字符串比較中,可以直接使用 > < 無需轉義 1. [[ ]] 內部可以使用 && || 進行邏輯運算 1. [[ ]] 是bash keyword:[[ is a shell keyword ## if 結構 ``` if commands; then commands [elif commands; then commands...] [else commands] fi // 多個條件 if [ commands ] && [ commands ] ;then // code fi ``` 示例 ``` if test $USER = "foo"; then echo "Hello foo." else echo "You are not foo." fi ``` ## test 命令 ``` # 寫法一 test expression # 寫法二 [ expression ] # 寫法三 [[ expression ]] ``` ``` # 寫法一 if test -e /tmp/foo.txt ; then echo "Found foo.txt" fi # 寫法二 if [ -e /tmp/foo.txt ] ; then echo "Found foo.txt" fi # 寫法三 if [[ -e /tmp/foo.txt ]] ; then echo "Found foo.txt" fi ``` ### && || 邏輯運算 ``` $ command1 && command2 // command1 成功執行 command2 $ command1 || command2 // command1 失敗執行 command2 ``` 示例1 ``` [ -d temp ] || mkdir temp ``` 示例2 ``` [[ -d "$dir_name" ]] && cd "$dir_name" && rm * ``` ## 判斷表達式 ### 文件 / 目錄 判斷 * `[ -d file ]`:如果 file 存在并且是一個目錄,則為`true`。 * `[ -e file ]`:如果 file 存在,則為`true`。 * `[ -f file ]`:如果 file 存在并且是一個普通文件,則為`true`。 * `[ -r file ]`:如果 file 存在并且可讀(當前用戶有可讀權限),則為`true`。 * `[ -s file ]`:如果 file 存在且其長度大于零,則為`true`。 * `[ -w file ]`:如果 file 存在并且可寫(當前用戶擁有可寫權限),則為`true`。 * `[ -x file ]`:如果 file 存在并且可執行(有效用戶有執行/搜索權限),則為`true`。 * `[ file1 -nt file2 ]`:如果 FILE1 比 FILE2 的更新時間最近,或者 FILE1 存在而 FILE2 不存在,則為`true`。 * `[ file1 -ot file2 ]`:如果 FILE1 比 FILE2 的更新時間更舊,或者 FILE2 存在而 FILE1 不存在,則為`true`。 * `[ FILE1 -ef FILE2 ]`:如果 FILE1 和 FILE2 引用相同的設備和 inode 編號,則為`true`。 ### 字符串判斷 * `[ string ]`:如果`string`不為空(長度大于0),則判斷為真。 * `[ -n string ]`:如果字符串`string`的長度大于零,則判斷為真。 * `[ -z string ]`:如果字符串`string`的長度為零,則判斷為真。 * `[ string1 = string2 ]`:如果`string1`和`string2`相同,則判斷為真。 * `[ string1 == string2 ]`等同于`[ string1 = string2 ]`。 * `[ string1 != string2 ]`:如果`string1`和`string2`不相同,則判斷為真。 * `[ string1 '>' string2 ]`:如果按照字典順序`string1`排列在`string2`之后,則判斷為真。 * `[ string1 '<' string2 ]`:如果按照字典順序`string1`排列在`string2`之前,則判斷為真。 ### 整數判斷 * `[ integer1 -eq integer2 ]`:如果`integer1`等于`integer2`,則為`true`。 * `[ integer1 -ne integer2 ]`:如果`integer1`不等于`integer2`,則為`true`。 * `[ integer1 -le integer2 ]`:如果`integer1`小于或等于`integer2`,則為`true`。 * `[ integer1 -lt integer2 ]`:如果`integer1`小于`integer2`,則為`true`。 * `[ integer1 -ge integer2 ]`:如果`integer1`大于或等于`integer2`,則為`true`。 * `[ integer1 -gt integer2 ]`:如果`integer1`大于`integer2`,則為`true`。 ### 正則判斷 語法 `[[ string1 =~ regex ]]` 示例 ``` INT=-5 if [[ "$INT" =~ ^-?[0-9]+$ ]]; then echo "INT is an integer." exit 0 else echo "INT is not an integer." >&2 exit 1 fi ``` ### 算術判斷 ``` if ((3 > 2)); then echo "true" fi ``` ``` if [[ ! -d "$dir_name" ]]; then echo "No such directory: '$dir_name'" >&2 exit 1 fi if ! cd "$dir_name"; then echo "Cannot cd to '$dir_name'" >&2 exit 1 fi if ! rm *; then echo "File deletion failed. Check results" >&2 exit 1 fi ``` ## case 結構 格式 ``` case expression in pattern ) commands ;; pattern ) commands ;; ... esac ``` case的匹配模式可以使用各種通配符 ``` a):匹配a。 a|b):匹配a或b。 [[:alpha:]]):匹配單個字母。 ???):匹配3個字符的單詞。 *.txt):匹配.txt結尾。 *):匹配任意輸入,通過作為case結構的最后一個模式 ``` Bash 4.0之前,case結構只能匹配一個條件,然后就會退出case結構。Bash 4.0之后,允許匹配多個條件,這時可以用;;&終止每個條件塊 示例1 ``` #! /bin/bash echo -n "輸入1~3 的數字" read char case $char in 1)echo 1;; 2)echo 2;; 3)echo 3;; *)echo "輸入不符合要求" esac ``` 示例2 ``` #!/bin/bash OS=$(uname -s) case "$OS" in FreeBSD) echo "This is FreeBSD" ;; Darwin) echo "This is Mac OSX" ;; AIX) echo "This is AIX" ;; Minix) echo "This is Minix" ;; Linux) echo "This is Linux" ;; *) echo "Failed to identify this OS" ;; esac ``` 示例3 滿足條件后終止匹配 ``` read -n 1 -p "Type a character > " echo case $REPLY in [[:upper:]]) echo "'$REPLY' is upper case." ;;& [[:lower:]]) echo "'$REPLY' is lower case." ;;& [[:alpha:]]) echo "'$REPLY' is alphabetic." ;;& esac ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看