編寫Linux指令碼,linux指令碼
以下是重啟Linux下某進程的shell指令碼,以tomcat進程為例:
#!/bin/shpid=`ps -ef|grep tomcat|grep -v grep|awk '{print $2}'`if [ "$pid" = "" ] ; then echo "tomcat service does not start!"else kill -9 $pid pid1=`ps -ef|grep tomcat|grep -v grep|awk '{print $2}'` if [ "$pid1" = "" ] ; then echo "Successfully kill tomcat processes: " $pid else echo "tomcat kill process failed!" exit 1 fifirm -rf /opt/tomcat/work/*./startup.shpid2=`ps -ef|grep tomcat|grep -v grep|awk '{print $2}'`if [ "$pid2" = "" ] ; then echo "tomcat service failed to start!"else echo "tomcat service starts successfully:" $pid2fi
Linux怎編寫shell指令碼?
一般以
#!/bin/sh
開頭(不是必須要寫,但一定要單獨一行),指定執行這個指令碼的shell程式(也可以用#!/bin/zsh或其他),然後就是堆命令了。Linux的shell指令碼支援很多功能,加上Linux高度模組化的命令,完全可以用shell指令碼寫出複雜的程式。
以上只是簡單介紹如何開始寫shell指令碼,如果要寫複雜的指令碼,還需要深入學習相關知識(如if——fi、case——esac等結構)。
當然,還需要給指令碼加上可執行許可權(chmod +x ./file.sh),否則可以用
sh ./file.sh
方式執行指令碼(這裡的sh是執行指令碼所需shell,命令也可以是zsh ./file.sh或其他)。
linux 指令碼編寫
我也來一個, 意思差不多就是用了sed處理
區分大小寫
1 #!/bin/bash
2
3 [ -z $1 ] && { echo "error"; exit 1; }
4
5 var=$1
6 grep "$1" /root/cmdline.txt | sed 's/^.*=//'
7
8 exit 0
##############################
補充
to 樓上
對你的方法略作最佳化
inside=`awk -F "=" '$1~/INSIDE/{print $2}' </root/cmdline.txt`
echo $inside