1. 獲取腳本當前路徑:
FILE_DIR=`echo $(cd $(dirname $0); pwd)`
2. shell整數計算可用expr,非整數計算可用awk內置函數實現:
四捨五入:
awk BEGIN'{printf("%d", 1.7+0.5)}'
保留小數:
awk BEGIN'{printf "%.2f\n", 2/3}'
3. (())
while read line
do
((lineNum++))
done < inputfile
4. 在文件中查找關鍵字時,用grep -c XXX 比grep XXX | wc -l 的效率要高,
但是查找進程時,ps -ef | grep XXX | grep -v grep |wc -l 才是對的,因為要過濾掉grep進程
5. grep -A4 '#!/bin/bash' dir.sh #-A4 代表顯示前四行
6. cut命令
linux-0xvi:/opt/Program # echo abcdefghi | cut -b2-5
bcde
linux-0xvi:/opt/Program # echo abcdefghi | cut -c2-5
bcde
linux-0xvi:/opt/Program # echo abc:def:hij:kmn | cut -d: -f2-3
def:hij
7.linux捕獲信號:
linux-0xvi:/opt/Program # cat signal.sh
#!/bin/bash
function handle
{
echo "received a signal"
}
trap 'handle' HUP INT QUIT TSTP
sleep 5
8.用stty和dd實現暫停
linux-0xvi:/opt/Program # cat press.sh
#!/bin/sh
function char
{
settty=$(stty -g)
stty raw
stty -echo
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty echo
stty -raw
stty $settty
}
echo -n "press any key to continue..."
input=`char`
9. perl -e 'print time'
10. 一條命令建立目錄樹: mkdir -p test/{inc,src,help/{html,pdf,doc}}