> For the complete documentation index, see [llms.txt](https://stb11816.gitbook.io/python_note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stb11816.gitbook.io/python_note/ubuntu/shell-script.md).

# shell script

介紹bash[\
http://linux.vbird.org/linux\_basic/0320bash.php](http://linux.vbird.org/linux_basic/0320bash.php)

介紹shell script[\
http://linux.vbird.org/linux\_basic/0340bashshell-scripts.php](http://linux.vbird.org/linux_basic/0340bashshell-scripts.php)

```
# 變更sh檔案的權限，使其可被執行
sudo chmod +x [file]
```

參考連結：\
<https://blog.twtnn.com/2013/12/shell-script.html>\
<https://blog.csdn.net/u012373815/article/details/62076793>

排程範例

```
!/bin/bash
cd `dirname $0`
python3 daily_crawler_ckip.py
```

## 讀取yaml

<https://gist.github.com/pkuczynski/8665367>

<http://landcareweb.com/questions/2181/ru-he-cong-linux-shelljiao-ben-jie-xi-yamlwen-jian>

## import function

開頭/bin/sh或/bin/bash有差

bash[\
https://ithelp.ithome.com.tw/articles/10138849](https://ithelp.ithome.com.tw/articles/10138849)

sh[\
https://stackoverflow.com/questions/13702425/source-command-not-found-in-sh-shell](https://stackoverflow.com/questions/13702425/source-command-not-found-in-sh-shell)

## #!/bin/bash vs #!/bin/sh

定義此腳本選擇哪種shell來執行

**#!/bin/sh是#!/bin/bash的缩减版。**

<https://blog.csdn.net/u010486679/article/details/78534841>

## exit 狀態碼

<https://blog.csdn.net/hongkangwl/article/details/16184883>

## 取得 Script 所在目錄位置

可以確保位置鎖定在sh檔案所在的目錄，而非執行sh的當下路徑

$0 變數代表指令的第一個參數, 即 Shell Script 本身

```bash
#!/bin/sh
echo $0

BASEDIR=$(dirname "$0")
echo "$BASEDIR"
# 如果用相對路徑執行, 只會返回相對路徑的目錄


filepath=$(cd "$(dirname "$0")"; pwd) 
echo "filepath: $filepath"
# 相對或絕對都可用
```

可藉由cd $BASEDIR至該script的位置

若要取得目錄：\
<https://www.opencli.com/linux/shell-script-get-script-location>\
<https://blog.csdn.net/LGD_2008/article/details/45913957>

## 特殊變數

|   變數   |            意義            |
| :----: | :----------------------: |
|   $$   |        shell本身的pid       |
|   $!   |      shellshell後端pid     |
|   $?   |      命令執行結束的編碼(有返回值)     |
|   $-   |      使用set命令設定的flag      |
|   $\*  |       列出所有傳遞給sh的參數       |
|   $@   |       列出所有傳遞給sh的參數       |
|   $#   |        傳遞給sh的參數個數        |
|   $0   |          sh檔案名稱          |
| $1\~$n | 傳遞給sh的參數，$1代表第一個，$2代表第二個 |

測試範本，執行時輸入test.sh aa

```bash
#!/bin/sh
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "argume:$@"
```

<https://blog.csdn.net/slovyz/article/details/47400107>

上一個指令最後可加上&，可用$!獲得pid

### $\* vs $@

```bash
#當不用雙引號時，都以"$1" "$2" … "$n" 的形式输出所有参数。
echo "\$*=" $*
echo "\"\$*\"=" "$*"

#使用雙引號時，"$*"會將所有參數視為一個整體，而"$@"則視為分開
echo "print each param from \"\$*\""
for var in "$*"
do
    echo "$var"
done
echo "print each param from \"\$@\""
for var in "$@"
do
    echo "$var"
done
```

<http://c.biancheng.net/cpp/view/2739.html>

## 特殊符號

&：在指令後面加上 & 符別, 即表示指令在背景執行, 例如 my-script.sh &

&&：第一道指令執行成功後, 才會執行第二道指令, 例如 make && make install

\| ：將第一道指令的輸出, 作為第二道指令的輸入, 例如 ls | grep filename

|| ：第一道指令執行失敗後, 才會執行第二道指令, 例如 cat filename || echo “fail”

<https://www.opencli.com/linux/linux-and-andand>

## 數值比較大小

```bash
int=1
if [ $int -eq 1 ]; then
    echo "true"
else
    echo "false"
fi
```

-eq:意指兩個數值是否相等。

-ne:意指兩個數值是否不相等。

-gt:意指數值1是否大於數值2。

-lt:意指數值1是否小於數值2。

-ge:意指數值1是否大於等於數值2。

-le:意指數值1是否小於等於數值2。

<https://blog.csdn.net/huangjin0507/article/details/45045537>

<https://dreamtails.pixnet.net/blog/post/27881990>

## echo

\# 加上 -n 可以不斷行繼續在同一行顯示

<http://boson4.phys.tku.edu.tw/UNIX/Unix%20Command/unix_shell.htm>

## wait

等待全部或指定的處理程序執行結束

wait 指令可使 shell 暫停執行，直到指定識別碼為 n 的處理程序執行完畢或是所有的幕後處理程序執行完畢後才繼續 shell 的處理工作。shell 會去執行 wait 指令而不會產生另一個新的處理程序。

<http://boson4.phys.tku.edu.tw/UNIX/Unix%20Command/unix_shell.htm>

## 單引號、雙引號、無引號

**單引號：**

可以說是所見即所得：即將單引號內的內容原樣輸出，或者描述為單引號裡面看到的是什麼就會輸出什麼。

**雙引號：**

把雙引號內的內容輸出出來；如果內容中有命令、變數等，會先把變數、命令解析出結果，然後在輸出最終內容來。

**不加引號：**

不會將含有空格的字串視為一個整體輸出, 如果內容中有命令、變數等，會先把變數、命令解析出結果，然後在輸出最終內容來，如果字串中帶有空格等特殊字元，則不能完整的輸出，需要改加雙引號，一般連續的字串，數字，路徑等可以用。

<https://www.itread01.com/content/1549156529.html>
