scripts 在運行之前,最怕的就是出現語法錯誤的問題了!那么我們如何 debug 呢?有沒有辦法不需要透過直接運行該 scripts 就可以來判斷是否有問題呢?呵呵!當然是有的!我們就直接以 bash 的相關參數來進行判斷吧!
[root@www ~]# sh [-nvx] scripts.sh 選項與參數: -n :不要運行 script,僅查詢語法的問題; -v :再運行 sccript 前,先將 scripts 的內容輸出到螢幕上; -x :將使用到的 script 內容顯示到螢幕上,這是很有用的參數! 范例一:測試 sh16.sh 有無語法的問題? [root@www ~]# sh -n sh16.sh # 若語法沒有問題,則不會顯示任何資訊! 范例二:將 sh15.sh 的運行過程全部列出來~ [root@www ~]# sh -x sh15.sh + PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin + export PATH + for animal in dog cat elephant + echo 'There are dogs.... ' There are dogs.... + for animal in dog cat elephant + echo 'There are cats.... ' There are cats.... + for animal in dog cat elephant + echo 'There are elephants.... ' There are elephants....
轉自 http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_6.php