python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page
  • 讀取yaml
  • import function
  • #!/bin/bash vs #!/bin/sh
  • exit 狀態碼
  • 取得 Script 所在目錄位置
  • 特殊變數
  • $* vs $@
  • 特殊符號
  • 數值比較大小
  • echo
  • wait
  • 單引號、雙引號、無引號

Was this helpful?

  1. Linux

shell script

Previous常用指令Nextsshfs

Last updated 5 years ago

Was this helpful?

介紹bash

介紹shell script

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

參考連結:

排程範例

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

讀取yaml

import function

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

bash

sh

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

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

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

exit 狀態碼

取得 Script 所在目錄位置

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

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

#!/bin/sh
echo $0

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


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

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

特殊變數

變數

意義

$$

shell本身的pid

$!

shellshell後端pid

$?

命令執行結束的編碼(有返回值)

$-

使用set命令設定的flag

$*

列出所有傳遞給sh的參數

$@

列出所有傳遞給sh的參數

$#

傳遞給sh的參數個數

$0

sh檔案名稱

$1~$n

傳遞給sh的參數,$1代表第一個,$2代表第二個

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

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

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

$* vs $@

#當不用雙引號時,都以"$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

特殊符號

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

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

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

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

數值比較大小

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。

echo

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

wait

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

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

單引號、雙引號、無引號

單引號:

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

雙引號:

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

不加引號:

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

若要取得目錄:

http://linux.vbird.org/linux_basic/0320bash.php
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php
https://blog.twtnn.com/2013/12/shell-script.html
https://blog.csdn.net/u012373815/article/details/62076793
https://gist.github.com/pkuczynski/8665367
http://landcareweb.com/questions/2181/ru-he-cong-linux-shelljiao-ben-jie-xi-yamlwen-jian
https://ithelp.ithome.com.tw/articles/10138849
https://stackoverflow.com/questions/13702425/source-command-not-found-in-sh-shell
https://blog.csdn.net/u010486679/article/details/78534841
https://blog.csdn.net/hongkangwl/article/details/16184883
https://www.opencli.com/linux/shell-script-get-script-location
https://blog.csdn.net/LGD_2008/article/details/45913957
https://blog.csdn.net/slovyz/article/details/47400107
http://c.biancheng.net/cpp/view/2739.html
https://www.opencli.com/linux/linux-and-andand
https://blog.csdn.net/huangjin0507/article/details/45045537
https://dreamtails.pixnet.net/blog/post/27881990
http://boson4.phys.tku.edu.tw/UNIX/Unix%20Command/unix_shell.htm
http://boson4.phys.tku.edu.tw/UNIX/Unix%20Command/unix_shell.htm
https://www.itread01.com/content/1549156529.html