1. 指派語句之間不能有空格
CPU_9G25="9G25"
CPU_TYPE=${CPU_9G25}
2. if條件或語句(-o)
if [ ${CPU_TYPE} = ${CPU_NUC972} -o ${CPU_TYPE} = ${CPU_NUC977} ]; then
#...
fi
注意空格,或(-o)
或者按照這個格式編寫
if [ ${CPU_TYPE} = ${CPU_NUC972} ] || [ ${CPU_TYPE} = ${CPU_NUC977} ]; then
#...
fi
3. 判斷兩個字元串是否相等(注意字元串比較為=,它兩側有空格)
CPU_NUC977="NUC977"
CPU_TYPE="NUC977"
if [ ${CPU_TYPE} = ${CPU_NUC977} ]; then
#...
fi
4. 判斷一個目錄是否存在(參數: -d)
RAMPPPD="/etc/pppd"
if [ ! -d $RAMPPPD ]; then
mount -t ramfs ramfs $RAMPPPD
fi
5. 判斷一個檔案是否存在(參數: -f)
RAMPPPD="/etc/pppd/hello"
if [ ! -f $RAMPPPD ]; then
mount -t ramfs ramfs $RAMPPPD
fi
6. 擷取記憶體空間
cat /proc/meminfo | grep "MemTotal" > /tmp/ramsize
eval $(awk 'NR=1 {printf("RAM_SIZE=%s;", $2)}' /tmp/ramsize)
echo "ram size:" ${RAM_SIZE}
7. 擷取cpu型号
cat /proc/cpuinfo | grep "Hardware" > /tmp/cputype
eval $(awk 'NR=1 {printf("CPU_TYPE=%s;", $3)}' /tmp/cputype)
echo "cpu type:" $CPU_TYPE
8. 判斷一個程序是否存在
APPCON_NAME="AppCon"
ProcNumber=`ps -ef |grep $APPCON_NAME|grep -v grep|wc -l`
if [ $ProcNumber -le 0 ]; then
/mnt/app/AppCon on &
echo "Start console AppCon."
fi
9. 數字大于比較(參數:-gt)
RAM_SIZE=90000
if [ ${RAM_SIZE} -gt 60000 ]; then
#......
fi
10. set顯示一段腳本的内容
去追蹤一段代碼的顯示情況,執行後在整個腳本有效
set -x #開啟
#需要執行的腳本内容
set +x #關閉
set -o #檢視
11. shell腳本中執行的指令内容輸出到變量中(如tty輸出值的bi)
#!/bin/sh
if [ $(tty) = "/dev/ttyUSB0" ]; then
tty
fi
12. 将指令的結果輸出到變量,需用單引号‘’
#!/bin/sh
workdir=`pwd` #執行指令時用單引号``
echo "workdir:"${workdir}
13. 輸出目前目錄路徑的兩種方式
a.
#!/bin/sh
workdir=`pwd`
echo "workdir:"${workdir}
b.
#!/bin/sh
workdir=$(cd $(dirname $0); pwd)
echo $workdir