在编写shell脚本的时候,难免会使用shell脚本的字符串拼接,不经常使用的话真的会忘记。本人写着一篇的目的也就是记录以下,到时候回过头来不用找的太麻烦。
首先变量与变量拼接
str1="123" str2="456" echo $str1$str2
结果输出123456
其次字符串与变量拼接
str3="789" echo $str3"000"
输出结果789000
另外一种变量与字符串拼接方式
echo "${str3}000"
输出结果789000
再其次字符串与字符串的拼接
echo "111""222"
输出结果111222
再其次linux命令与字符串拼接
echo "[`date` ]"
这里使用linux 的date日期命令
输出结果为[Tue May 26 16:03:36 CST 2020 ]
再其次linux命令与变量拼接
echo `date`$str1
输出结果为:Tue May 26 16:05:54 CST 2020123
最后命令与命令的拼接
echo `date``ls`
输出结果为:Tue May 26 16:07:36 CST 2020auto.sh logs logsetting.sh logtar.sh lua luajit openResty.conf shelltest.sh src
以上借本包括了所有的shell脚本的字符串拼接,希望对自己和大家都有用。最后附上整体脚本和输出结果截图
#字符串与字符串的拼接 str1="123" str2="456" echo $str1$str2 #变量和字符串拼接 str3="789" echo $str3"000" echo "${str3}000" #字符串与字符串 echo "111""222" #字符串与命令 echo "[`date` ]" #命令与变量 echo `date`$str1 #命令与命令 echo `date``ls`
进程的健康检查脚本如下:
1 ps -A -ostat,ppid,pid,cmd |grep -e '^[Z]' | grep -e $PID_NAME | while read line 2 do 3 for col in $line; do 4 if [[ $col == $PID_NAME":" ]]; then 5 echo "equal nginx" $father_pid 6 #kill -9 $father_pid 7 #kill -HUP $father_pid 8 fi 9 father_pid=$col 10 done 11 echo $line 12 done
说明:
PID_NAME 待过滤的进程名字,这里我哦过滤nginx
执行效果如图所示: