編寫Linux指令碼,linux指令碼

來源:互聯網
上載者:User

編寫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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.