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
https://blog.youkuaiyun.com/csfreebird/article/details/7978699#reply
很详细,写的太复杂了!
https://www.cnblogs.com/xuxm2007/archive/2011/10/20/2218846.html
简单清晰!!
https://blog.youkuaiyun.com/ztf312/article/details/52317571