shell的if嵌套


shell腳本編寫中,if是可以嵌套的, 
注意,if條件表達式[]中的空格,以及后面的then就行了 
if條件表達式后面的分號“;”可有可無,不影響。

代碼如下

myFile='ctr_result.txt'
if [ -e "$myFile" ]; then
        rm $myFile
        result_code=$?
        if [ "$result_code" -ne 0 ]; then
                echo "[ERROR] when delete $myFile"
                exit $result_code
        fi
        echo "$myFile exists, we delete it"
fi


  
 

  

 

實際shell 腳本


if [ $packetanal_ver != 'yechang' ];then
     packetpid=`ps aux|grep  packetAnal|grep -v "grep"|awk '{print $2}'`

     if [ "$packetpid" ];then
          kill -9 $packetpid
          echo "old packetAnal is killed"
     fi
fi

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM