判断程序有没有运行 ,没有则启动程序
#!/bin/sh# 替换为你要检查的程序名称
program_name="xxx"# 使用pgrep命令查找程序的进程ID
pid=$(pgrep -f "$program_name")# 如果没有找到进程ID,说明程序没有运行,启动程序
if [ -z "$pid" ]; thenecho "程序未运行,正在启动..."# 替换为你要启动的程序命令
elseecho "程序已在运行,进程ID为:$pid"
fi
while循环
#!/bin/sh
a=0while [ "$a" -lt 20 ] # this is loop1
doecho $aa=`expr $a + 1`
done
系统运行时间
cat /proc/uptime
用于查看系统启动后的运行时间。它会显示两个时间值,第一个是系统启动后的总运行时间,第二个是表示系统所有cpu空闲状态下累计花费的时间。
数值比较
-eq //等于
-ne //不等于
-gt //大于 (greater)
-lt //小于 (less)
-ge //大于等于
-le //小于等于
#!/bin/bash
#文件名:test.sh
one=30
two=40
if [ $one -ge $two ]
thenecho $one"大于"$two
elseecho $one"小于"$two
fi
该方法只能对整数进行比较
浮点数比较
方法1:使用bc
#!/bin/bash
#文件名:test.sh
one=30.1
two=40.1
compare=$(echo "$one > $two" | bc -l)
if [ $compare -eq 1 ]
thenecho $one"大于"$two
elseecho $one"小于"$two
fi
方法2:使用printf
与expr
#!/bin/shfloat1=3.14
float2=2.17# 使用printf格式化浮点数,并使用expr进行比较
diff=$(printf "%.2f\n" "$float1" | tr -d '.')
diff2=$(printf "%.2f\n" "$float2" | tr -d '.')
if [ $(expr $diff '>' $diff2) -eq 1 ]; thenecho "$float1 is greater than $float2"
elseecho "$float1 is not greater than $float2"
fi
对字符串进行校验
echo -n $val | md5sum | awk '{print $1}
echo 参数
-n 表示不输出换行符