例如一个脚本dumb.sh的内容为
#! /bin/bash
idx=1
echo $idx
/bin/bash -x dumb.sh的结果为:
+idx=1
+echo 1
1
而且/bin/bash -x dumb.sh > trace.log后,trace.log内容仅为:
1
有什么办法将/bin/bash -x dumb.sh的整个结果保存下来么?
伊谢尔伦2017-04-17 13:41:14
Processing method:
/bin/bash -x dump.sh > trace.log 2>&1
Reason:
Trace it with strace
strace /bin/bash -x dump.sh
Result:
write(2, "+ idx=1\n", 8+ idx=1
) = 8
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
write(2, "+ echo 1\n", 9+ echo 1
) = 9
write(1, "1\n", 21
) = 2
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
read(255, "", 29) = 0
exit_group(0) = ?
It can be seen that +idx=1 and +echo1 are write(2,), which is the standard error output. Although it is displayed on the screen, > this content will not be written to the file by default.
The following command can redirect standard error to 1
/bin/bash -x dump.sh > aa 2>&1
怪我咯2017-04-17 13:41:14
In the man bash manual chapter: CONDITIONAL EXPRESSIONS
, -x
means:
-x file
True if file exists and is executable.
The parameter -x
will list the order in which the shell executes statements and print them. +echo 1
This kind of statement is not the output of the script, so it cannot be saved to a file.