2015/7/8

Trace shell script的小技巧

參考上圖,有三個script,先執行test1.sh,會帶起test2.sh;test2.sh會再帶起test3.sh,如果從test1.sh開始了解程式架構,很容易就可以得知彼此之間的執行關係;但有時候線頭只抓到test2.sh或是test3.sh時就麻煩了。
  在Linux下可以使用bash maintain的$PPID變數再搭配ps指令就可以慢慢推敲出script file的呼叫關係。

直接看範例:
#!/bin/sh
# file: test1.sh

echo $0
echo "Execute by `ps -o comm -h --pid $PPID`"
echo ===========
./test2.sh

#!/bin/sh
# file: test2.sh

echo $0
echo "Execute by `ps -o comm -h --pid $PPID`"
echo ===========
./test3.sh

#!/bin/sh
# file: test3.sh

echo $0
echo "Execute by `ps -o comm -h --pid $PPID`"
echo ===========

執行結果:
./test1.sh
Execute by bash
===========
./test2.sh
Execute by test1.sh
===========
./test3.sh
Execute by test2.sh
===========

沒有留言:

張貼留言