我有一個在bash
中按預期運行的腳本,但是當它被轉移到運行busybox
的shell下ash
shell時,它不會按照我的意願執行操作。我寫了2個簡單的腳本。 myscript1.sh:對於忙碌的灰殼,有相當於exec 5>/BASH_XTRACEFD =「5」/ set -x嗎?
#!/bin/bash
exec 5> debug_output.txt
BASH_XTRACEFD="5"
set -x
PS4='$LINENO: '
var1=blah
var2=foo
echo Currently in $0
echo
echo $0 :: var1 : $var1, var2 : $var2
export var1
echo
echo Entering myscript2.sh
./myscript2.sh
echo
echo Back in $0
echo $0 :: var1 : $var1, var2 : $var2
echo
和myscript2.sh:
#!/bin/bash
echo Currently in $0
echo
echo $0 :: var1 : $var1, var2 : $var2
var1=flop
var2=bleh
我寫了他們唯一的測試日誌記錄功能。前三行myscript1.sh創建一個名爲debug_output.txt的文件,並在運行時輸出腳本的輸出。這是myscript1.sh的輸出:
(^)#(^)#(^)#(^)[email protected]:~/bashscripttutorial$ ./myscript1.sh
Currently in ./myscript1.sh
./myscript1.sh :: var1 : blah, var2 : foo
Entering myscript2.sh
Currently in ./myscript2.sh
./myscript2.sh :: var1 : blah, var2 :
Back in ./myscript1.sh
./myscript1.sh :: var1 : blah, var2 : foo
(^)#(^)#(^)#(^)[email protected]:~/bashscripttutorial$ ls
debug_output.txt myscript1.sh myscript1.sh~ myscript2.sh myscript2.sh~
正如你可以看到debug_output.txt創建日誌文件,這裏是輸出:
(^)#(^)#(^)#(^)[email protected]:~/bashscripttutorial$ cat debug_output.txt
+ PS4='$LINENO: '
8: var1=blah
9: var2=foo
11: echo Currently in ./myscript1.sh
12: echo
13: echo ./myscript1.sh :: var1 : blah, var2 : foo
15: export var1
16: echo
17: echo Entering myscript2.sh
18: ./myscript2.sh
19: echo
20: echo Back in ./myscript1.sh
21: echo ./myscript1.sh :: var1 : blah, var2 : foo
22: echo
(^)#(^)#(^)#(^)[email protected]:~/bashscripttutorial$
來點破事兒是我可以準確看到腳本中發生的事情以及發生的行號。在busybox這是一個不同的故事。下面是myscript1.sh:
# cat myscript1.sh
#!/bin/bash
exec 5> debug_output.txt
BASH_XTRACEFD="5"
set -x
PS4='$LINENO: '
var1=blah
var2=foo
echo Currently in $0
echo
echo $0 :: var1 : $var1, var2 : $var2
export var1
echo
echo Entering myscript2.sh
./myscript2.sh
echo
echo Back in $0
echo $0 :: var1 : $var1, var2 : $var2
echo
和這裏的myscript2.sh:
# cat myscript2.sh
: cat myscript2.sh
#!/bin/bash
echo Currently in $0
echo
echo $0 :: var1 : $var1, var2 : $var2
var1=flop
var2=bleh
這是從運行myscript1.sh輸出:
# ./myscript1.sh
: ./myscript1.sh
+ PS4=$LINENO:
: var1=blah
: var2=foo
: echo Currently in ./myscript1.sh
Currently in ./myscript1.sh
: echo
: echo ./myscript1.sh :: var1 : blah, var2 : foo
./myscript1.sh :: var1 : blah, var2 : foo
: export var1
: echo
: echo Entering myscript2.sh
Entering myscript2.sh
: ./myscript2.sh
Currently in ./myscript2.sh
./myscript2.sh :: var1 : blah, var2 :
: echo
: echo Back in ./myscript1.sh
Back in ./myscript1.sh
: echo ./myscript1.sh :: var1 : blah, var2 : foo
./myscript1.sh :: var1 : blah, var2 : foo
: echo
調試.output.txt是創造但它是一個空文件。有沒有辦法像bash
那樣獲得類似的輸出結果?
當您在busybox中運行時,是否將'shebang line'(第1行)更改爲了「#!/ bin/sh」? (並不是說會解決這個問題)。祝你好運。 – shellter
我照你的建議做了,但沒有奏效。 –