<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # [X分鐘速成Y](http://learnxinyminutes.com/) ## 其中 Y=bash 源代碼下載:?[LearnBash-cn.sh](http://learnxinyminutes.com/docs/files/LearnBash-cn.sh) Bash 是一個為 GNU 計劃編寫的 Unix shell,是 Linux 和 Mac OS X 下的默認 shell。 以下大多數例子可以作為腳本的一部分運行,也可直接在 shell 下交互執行。 [更多信息](http://www.gnu.org/software/bash/manual/bashref.html) ~~~ #!/bin/bash # 腳本的第一行叫 shebang,用來告知系統如何執行該腳本: # 參見: http://en.wikipedia.org/wiki/Shebang_(Unix) # 如你所見,注釋以 # 開頭,shebang 也是注釋。 # 顯示 “Hello world!” echo Hello world! # 每一句指令以換行或分號隔開: echo 'This is the first line'; echo 'This is the second line' # 聲明一個變量: Variable="Some string" # 下面是錯誤的做法: Variable = "Some string" # Bash 會把 Variable 當做一個指令,由于找不到該指令,因此這里會報錯。 # 也不可以這樣: Variable= 'Some string' # Bash 會認為 'Some string' 是一條指令,由于找不到該指令,這里再次報錯。 # (這個例子中 'Variable=' 這部分會被當作僅對 'Some string' 起作用的賦值。) # 使用變量: echo $Variable echo "$Variable" echo '$Variable' # 當你賦值 (assign) 、導出 (export),或者以其他方式使用變量時,變量名前不加 $。 # 如果要使用變量的值, 則要加 $。 # 注意: ' (單引號) 不會展開變量(即會屏蔽掉變量)。 # 在變量內部進行字符串代換 echo ${Variable/Some/A} # 會把 Variable 中首次出現的 "some" 替換成 “A”。 # 變量的截取 Length=7 echo ${Variable:0:Length} # 這樣會僅返回變量值的前7個字符 # 變量的默認值 echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} # 對 null (Foo=) 和空串 (Foo="") 起作用; 零(Foo=0)時返回0 # 注意這僅返回默認值而不是改變變量的值 # 內置變量: # 下面的內置變量很有用 echo "Last program return value: $?" echo "Script's PID: $$" echo "Number of arguments: $#" echo "Scripts arguments: $@" echo "Scripts arguments separated in different variables: $1 $2..." # 讀取輸入: echo "What's your name?" read Name # 這里不需要聲明新變量 echo Hello, $Name! # 通常的 if 結構看起來像這樣: # 'man test' 可查看更多的信息 if [ $Name -ne $USER ] then echo "Your name isn't your username" else echo "Your name is your username" fi # 根據上一個指令執行結果決定是否執行下一個指令 echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" && echo "Only executed if first command does NOT fail" # 在 if 語句中使用 && 和 || 需要多對方括號 if [ $Name == "Steve" ] && [ $Age -eq 15 ] then echo "This will run if $Name is Steve AND $Age is 15." fi if [ $Name == "Daniya" ] || [ $Name == "Zach" ] then echo "This will run if $Name is Daniya OR Zach." fi # 表達式的格式如下: echo $(( 10 + 5 )) # 與其他編程語言不同的是,bash 運行時依賴上下文。比如,使用 ls 時,列出當前目錄。 ls # 指令可以帶有選項: ls -l # 列出文件和目錄的詳細信息 # 前一個指令的輸出可以當作后一個指令的輸入。grep 用來匹配字符串。 # 用下面的指令列出當前目錄下所有的 txt 文件: ls -l | grep "\.txt" # 重定向輸入和輸出(標準輸入,標準輸出,標準錯誤)。 # 以 ^EOF$ 作為結束標記從標準輸入讀取數據并覆蓋 hello.py : cat > hello.py << EOF #!/usr/bin/env python from __future__ import print_function import sys print("#stdout", file=sys.stdout) print("#stderr", file=sys.stderr) for line in sys.stdin: print(line, file=sys.stdout) EOF # 重定向可以到輸出,輸入和錯誤輸出。 python hello.py < "input.in" python hello.py > "output.out" python hello.py 2> "error.err" python hello.py > "output-and-error.log" 2>&1 python hello.py > /dev/null 2>&1 # > 會覆蓋已存在的文件, >> 會以累加的方式輸出文件中。 python hello.py >> "output.out" 2>> "error.err" # 覆蓋 output.out , 追加 error.err 并統計行數 info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err wc -l output.out error.err # 運行指令并打印文件描述符 (比如 /dev/fd/123) # 具體可查看: man fd echo <(echo "#helloworld") # 以 "#helloworld" 覆蓋 output.out: cat > output.out <(echo "#helloworld") echo "#helloworld" > output.out echo "#helloworld" | cat > output.out echo "#helloworld" | tee output.out >/dev/null # 清理臨時文件并顯示詳情(增加 '-i' 選項啟用交互模式) rm -v output.out error.err output-and-error.log # 一個指令可用 $( ) 嵌套在另一個指令內部: # 以下的指令會打印當前目錄下的目錄和文件總數 echo "There are $(ls | wc -l) items here." # 反引號 `` 起相同作用,但不允許嵌套 # 優先使用 $( ). echo "There are `ls | wc -l` items here." # Bash 的 case 語句與 Java 和 C++ 中的 switch 語句類似: case "$Variable" in # 列出需要匹配的字符串 0) echo "There is a zero.";; 1) echo "There is a one.";; *) echo "It is not null.";; esac # 循環遍歷給定的參數序列: # 變量$Variable 的值會被打印 3 次。 for Variable in {1..3} do echo "$Variable" done # 或傳統的 “for循環” : for ((a=1; a <= 3; a++)) do echo $a done # 也可以用于文件 # 用 cat 輸出 file1 和 file2 內容 for Variable in file1 file2 do cat "$Variable" done # 或作用于其他命令的輸出 # 對 ls 輸出的文件執行 cat 指令。 for Output in $(ls) do cat "$Output" done # while 循環: while [ true ] do echo "loop body here..." break done # 你也可以使用函數 # 定義函數: function foo () { echo "Arguments work just like script arguments: $@" echo "And: $1 $2..." echo "This is a function" return 0 } # 更簡單的方法 bar () { echo "Another way to declare functions!" return 0 } # 調用函數 foo "My name is" $Name # 有很多有用的指令需要學習: # 打印 file.txt 的最后 10 行 tail -n 10 file.txt # 打印 file.txt 的前 10 行 head -n 10 file.txt # 將 file.txt 按行排序 sort file.txt # 報告或忽略重復的行,用選項 -d 打印重復的行 uniq -d file.txt # 打印每行中 ',' 之前內容 cut -d ',' -f 1 file.txt # 將 file.txt 文件所有 'okay' 替換為 'great', (兼容正則表達式) sed -i 's/okay/great/g' file.txt # 將 file.txt 中匹配正則的行打印到標準輸出 # 這里打印以 "foo" 開頭, "bar" 結尾的行 grep "^foo.*bar$" file.txt # 使用選項 "-c" 統計行數 grep -c "^foo.*bar$" file.txt # 如果只是要按字面形式搜索字符串而不是按正則表達式,使用 fgrep (或 grep -F) fgrep "^foo.*bar$" file.txt # 以 bash 內建的 'help' 指令閱讀 Bash 自帶文檔: help help help help for help return help source help . # 用 mam 指令閱讀相關的 Bash 手冊 apropos bash man 1 bash man bash # 用 info 指令查閱命令的 info 文檔 (info 中按 ? 顯示幫助信息) apropos info | grep '^info.*(' man info info info info 5 info # 閱讀 Bash 的 info 文檔: info bash info bash 'Bash Features' info bash 6 info --apropos bash ~~~
                  <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>

                              哎呀哎呀视频在线观看