Skip to main content

範例與常用技巧

檔案的目錄位置
Script 檔案名稱
$ echo $0
./test.sh

$ echo `basename $0`
test.sh 
刪除 * 天前的舊備份檔
cd $BACKUP_DIR
for backup in `find . -ctime +$BACKUP_KEEP_DAYS -name "cacti.*.tar.gz"`; do rm -f $backup; done;
輸出多行的文字訊息
cat <<EOF
Welcome .....
	
Here are the messages that you want to show up
		
EOF
將執行後的所有輸出訊息導入一個檔案
#!/bin/sh
LOG="my.log"
(
....
) 2>&1 | tee -a $LOG 

不適用在內容裡有 python 指令的訊息輸入,輸入等待的畫面會無法顯示。

temp=$(mktemp)
exec &> ${temp}

echo "All outputs will be saved into the file ${temp}."
快速修改大量檔案的副檔名
## *.old -> *.new
for fname in $(ls *.old);do echo "mv $fname ->"; echo $(echo $fname |sed 's/.old/.new/');mv $fname $(echo $fname | sed 's/.old/.new/');done
在 SHELL 內執行另一個外部 SHELL 或指令

1. 使用 pipe line

echo"md5sum $X > $X.sum "| bash

2. 使用 eval

get_arch="uname -p"
if [ "`eval "$get_arch"`" = "i686" ]; then
....
fi 
命令模式提示字元的路徑名稱太長

加上這變數

PROMPT_DIRTRIM=2
shuf: 隨機排序
curl -s https://www.imdb.com/list/ls020046354/export | cut -d ',' -f 6  | shuf
tree: 顯示專案目錄的檔案樹
tree --dirsfirst --filelimit 10 --sort=name

# Display size of files
tree -s

# Display permissions of files
tree -p

# Display directory only
tree -d

# Display till a certain level/depth
tree -L 1

# List only those files that match pattern given
tree -P *screenshot*
lsof: 偵錯 process/port/NFS
# What process is using the port 50000
lsof -i :50000

# What process is using the mount-point
lsof | grep /path/to/mount-point
lsof +f -- /path/to/dir

# What files and directories are using by the process
lsof -p `pidof <app_name>`
sort : 資料排序
sort -t ',' -k5,5 -k1,1 -k9,9 -k3,3 -k11,11 my.csv
  •  -t 分隔符號
  • -k5,5 排序第 5 欄,以字串類型排列
  • 欄位排序先後依序為第 5, 1, 9, 3, 11 欄
  • 欄位排序若要以數值方式來排,改成 -k5,5n
timeout : 自動停止執行
timeout 10 tail -f /var/log/httpd/access.log
timeout 5m ping 8.8.8.8
timeout 300 tcpdump -n -w data.pcap

# Sending specific signal
# To get a list of all available signals, use the command kill -l .
timeout -s SIGKILL ping 8.8.8.8
variables : 變數
變數 說明
 $0  腳本檔名
 $1  第1個參數
 $2  第2個參數
 ${10}  第10個參數 #10
 $#  參數的個數
 $*  顯示所有參數 (作為一個字串)
 $@  顯示所有參數 (每個為一個獨立字串)
 ${$*}  傳遞到腳本中的參數的個數
 ${$@}  傳遞到腳本中的參數的個數
 $?  腳本結束後的傳回值
 $$  腳本的程序 ID
 $_  前個命令的最後一個參數
 $!  最後一個執行程序的 ID
u=${1:-root}  如果 $1 未指定,就賦予值 root
u=${USER:-foo}  如果 $USER 未指定,就賦予值 foo
len=${#var}  計算 $var 字串的長度

如果 $2 未指定或空值,輸出錯誤訊息

${varName?Error varName is not defined}
${varName:?Error varName is not defined or is empty}