Reputation: 107
I have a script that runs as expected in bash
but when it's transferred to a box running busybox
under the ash
shell it doesn't do what I'd like. I've written 2 simple scripts. 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
and myscript2.sh:
#!/bin/bash
echo Currently in $0
echo
echo $0 :: var1 : $var1, var2 : $var2
var1=flop
var2=bleh
I wrote them only to test the logging feature. The first 3 lines of myscript1.sh creates a file called debug_output.txt and has the output of the script as it runs. This is the output of myscript1.sh:
(^)#(^)#(^)#(^)bminter@ubuntu:~/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
(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$ ls
debug_output.txt myscript1.sh myscript1.sh~ myscript2.sh myscript2.sh~
As you can see the log file debug_output.txt was created and here is the output:
(^)#(^)#(^)#(^)bminter@ubuntu:~/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
(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$
The thing to point out is that I can see exactly what's happening in the script and on what line number it's happening. In busybox it's a different story. Here's 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
and here's 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
This is the output from running 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
debug.output.txt is created but it's an empty file. Is there a way to get a similar output like I do on the bash
side?
Upvotes: 2
Views: 912
Reputation: 2491
You try to use bash specific variable with ash, the shell of busybox, but this shell don't know them (LINENO and BASH_XTRACEFD).
When you call set -x , the trace is print on fd2 (standard error).
You don't seen that with bash because you tell to catch the output of set -x with BASH_XTRACEFD and you redirect this output on a file (debug_output.txt).
With busybox, you see the trace of set -x on the screen because the output of fd2 is not redirect on the file.
If you want to get this output on the file with busybox, you must redirect fd2.
exec 5> debug_output.txt
became
exec 2> debug_output.txt
I can't seen a way to replace the variable LINENO with ash but you can use anchor to remenber you where you are like :
echo Currently in $0>&2
at the start of myscript2.sh
Upvotes: 2