<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] > [阮一峰的參考](https://wangdoc.com/bash/grammar.html#-n%E5%8F%82%E6%95%B0) > [google shell 規范](https://google.github.io/styleguide/shellguide.html) ## 通用模板 ``` #!/bin/bash # # 描述文件內容 set -euo pipefail ### ### my-script — does one thing well ### ### Usage: ### my-script <input> <output> ### ### Options: ### <input> Input file to read. ### <output> Output file to write. Use '-' for stdout. ### -h Show this message. help() { sed -rn 's/^### ?//;T;p' "$0" } # 沒有參數,或參數為 -h if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then help exit 1 fi ####################################### # Delete a file in a sophisticated manner. # Arguments: # File to delete, a path. # Returns: # 0 if thing was deleted, non-zero on error. ####################################### function del_thing() { rm "$1" } # 所有錯誤消息都應轉到STDERR err() { echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } if ! do_something; then err "Unable to do_something" exit 1 fi # 推薦從main 開始 main() { func1() func2() } ``` ## 使用文件之前判斷是否存在,并進行異常處理 ``` # 判斷普通文件存在 if [[ ! -f 'a.txt' ]]; then touch 'a.txt' fi # 判斷文件夾存在 if [[ ! -d 'src' ]]; then echo 'src dir not found' exit 1 fi ``` ## 條件判斷 `[[...]]` 優于 `[...]` ``` if [[ "filename" == "f*" ]]; then echo "Match" fi ``` ## 函數內部使用局部變量 變量默認為全局,在函數中推薦使用局部變量 ``` fn () { local foo foo=1 } ``` ## 使用 $() 而不是 反引號 ## 使用 /dev/null 過濾輸出信息 `[expr] > /dev/null 2>&1` 命令解釋:重定向到空設備,并把標準錯誤輸出stderr也重定向為stdout。 注意, 2>&1 應該總是放在命令的末尾。 ## 文件名的通配符擴展 `./*` 優于 `*` 由于文件名可以以"-"開頭,所以用`./*`代替`*`來擴展通配符要安全得多 ``` bad rm -v * good rm -v ./* ``` ## 目錄不存在問題 ``` // bad cd $dir_name rm * //如果目錄則會刪除用戶下的文件 // good [[ -d $dir_name ]] && cd $dir_name && rm * ``` ## 命名空間 命名空間只是語義化的函數名,`function`可省略 ``` # Single function my_func() { … } # Part of a package,更加語義化的函數名 mypackage::my_func() { … } ```
                  <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>

                              哎呀哎呀视频在线观看