個人在工作時候大量地使用Shell scripts,透過它將服務所需要的環境自動化設定並帶起該服務,很多時候需要針對script執行失敗的原因進行修正。為了達到這個目的,單單使用echo指令印出執行時期的結果來找出失敗的原因是不夠的。以下記錄個人覺得非常實用的debug技巧跟大家分享。
Shell script前言
sh (or the Shell Command Language) is a programming language described by the POSIX standard. It has many implementations (ksh88, dash, ...). bash can also be considered an implementation of sh (see below).
以上那段話是出自shell - Difference between sh and bash - Stack Overflow,用來說明sh是一個標準,而實作這個標準的有ash(busybox)、bash、dash、zsh等等。(而也有很多人使用的fish shell目前並不相容與POSIX standard,故以下技巧不一定適用於fish shell)
技巧
xtrace功能