Shell腳本一些有用且很酷的東西
Fizer Khan是一位Shell腳本迷,他對有關Shell腳本新奇有趣的東西是如此的癡迷。最近他遇到了authy-ssh腳本,為了緩解ssh伺服器雙重認證問題,他學到了許多有用且很酷的東西。對此,他想分享給大家。
一、Colors your echo
大多數情況下,你希望輸出echo Color,比如綠色代表成功,紅色代表失敗,×××代表警告。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code>NORMAL=$(tput sgr0)</code>
<code>GREEN=$(tput setaf 2; tput bold)</code>
<code>YELLOW=$(tput setaf 3)</code>
<code>RED=$(tput setaf 1)</code>
<code>function</code><code>red() {</code>
<code>echo -e</code><code>"$RED$*$NORMAL"</code>
<code>}</code>
<code>function</code><code>green() {</code>
<code>echo -e</code><code>"$GREEN$*$NORMAL"</code>
<code>function</code><code>yellow() {</code>
<code>echo -e</code><code>"$YELLOW$*$NORMAL"</code>
<code># To print success</code>
<code>green</code><code>"Task has been completed"</code>
<code># To print error</code>
<code>red</code><code>"The configuration file does not exist"</code>
<code># To print warning</code>
<code>yellow</code><code>"You have to use higher version."</code>
這裡使用tput來設定顔色、文本設定并重置到正常顔色。想更多了解tput,請參閱prompt-color-using-tput。
二、To print debug information (列印調試資訊)
列印調試資訊隻需調試設定flag。
<code>function</code><code>debug() {</code>
<code>if</code><code>[[ $DEBUG ]]</code>
<code>then</code>
<code>echo</code><code>">>> $*"</code>
<code>fi</code>
<code># For any debug message</code>
<code>debug</code><code>"Trying to find config file"</code>
某些極客還會提供線上調試功能:
<code># From cool geeks at hacker news</code>
<code>function</code><code>debug() { ((DEBUG)) && echo</code><code>">>> $*"</code><code>; }</code>
<code>function</code><code>debug() { [</code><code>"$DEBUG"</code><code>] && echo</code><code>">>> $*"</code><code>; }</code>
三、To check specific executable exists or not (檢查特定可執行的檔案是否存在)
<code>OK=0</code>
<code>FAIL=1</code>
<code>function</code><code>require_curl() {</code>
<code>which curl &>/dev/</code><code>null</code>
<code>if</code><code>[ $? -eq 0 ]</code>
<code>return</code><code>$OK</code>
<code>return</code><code>$FAIL</code>
這裡使用which來指令查找可執行的curl 路徑。如果成功,那麼可執行的檔案存在,反之則不存在。将&>/dev/null設定在輸出流中,錯誤流會顯示to /dev/null (這就意味着在控制闆上沒有任何東西可列印)。
有些極客會建議直接通過傳回which來傳回代碼。
<code>function</code><code>require_curl() { which</code><code>"curl"</code><code>&>/dev/</code><code>null</code><code>; }</code>
<code>function</code><code>require_curl() { which -s</code><code>"curl"</code><code>; }</code>
四、To print usage of scripts (列印使用的腳本)
當我開始編寫shell 腳本,我會用echo來指令列印已使用的腳本。當有大量的文本在使用時, echo指令會變得淩亂,那麼可以利用cat來設定指令。
<code>cat << EOF</code>
<code>Usage: myscript <command> <arguments></code>
<code>VERSION: 1.0</code>
<code>Available Commands</code>
<code>install - Install package</code>
<code>uninstall - Uninstall package</code>
<code>update - Update package</code>
<code>list - List packages</code>
<code>EOF</code>
這裡的<<被稱為<<here document,字元串在兩個EOF中。
五、User configured value vs Default value (使用者配置值VS 預設值)
有時,如果使用者沒有設定值,那麼會使用預設值。
<code>URL=${URL:-http:</code><code>//localhost:8080}</code>
檢查URL環境變量。如果不存在,可指定為localhost。
六、To check the length of the string 檢查字元串長度
<code>if</code><code>[ ${</code><code>#authy_api_key} != 32 ]</code>
<code>red</code><code>"you have entered a wrong API key"</code>
利用 ${#VARIABLE_NAME} 定義變量值的長度。
七、To read inputs with timeout (讀取輸入逾時)
<code>READ_TIMEOUT=60</code>
<code>read -t</code><code>"$READ_TIMEOUT"</code><code>input</code>
<code># if you do not want quotes, then escape it</code>
<code>input=$(sed</code><code>"s/[;\`\"\$\' ]//g"</code><code><<< $input)</code>
<code># For reading number, then you can escape other characters</code>
<code>input=$(sed 's/[^0-9]*</code><code>//g' <<< $input)</code>
八、To get directory name and file name (擷取目錄名和檔案名)
<code># To find base directory</code>
<code>APP_ROOT=`dirname</code><code>"$0"</code><code>`</code>
<code># To find the file name</code>
<code>filename=`basename</code><code>"$filepath"</code><code>`</code>
<code># To find the file name without extension</code>
<code>filename=`basename</code><code>"$filepath"</code><code>.html`</code>
英文來源:What-I-learned-from-other-s-shell-scripts