linux 脚本编程

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值