Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Debugging bash:显示已执行的命令_Debugging_Bash_Command_Echo - Fatal编程技术网

Debugging bash:显示已执行的命令

Debugging bash:显示已执行的命令,debugging,bash,command,echo,Debugging,Bash,Command,Echo,我喜欢在bash中使用-x开关来调试脚本。唯一的缺点是,echo命令也会显示,这可能会产生大量不必要的重复: #!/bin/bash echo "Changing to /etc directory" cd /etc 然后运行一次: $ bash -x test.sh + echo 'Changing to /etc directory' Changing to /etc directory + cd /etc 我尝试使用bash-x test.sh | grep-v'+echo'进行过滤

我喜欢在bash中使用
-x
开关来调试脚本。唯一的缺点是,
echo
命令也会显示,这可能会产生大量不必要的重复:

#!/bin/bash
echo "Changing to /etc directory"
cd /etc
然后运行一次:

$ bash -x test.sh 
+ echo 'Changing to /etc directory'
Changing to /etc directory
+ cd /etc
我尝试使用
bash-x test.sh | grep-v'+echo'
进行过滤,但不起作用

[Q]有没有办法防止
-x
同时显示
echo
命令?


谢谢

您可以通过您选择的正则表达式引擎进行过滤:

$ ./test.sh 2>&1 | awk '{if(!match($0, /^\+ echo/)){print $0}}'


如果要过滤输出,只使用
grep-v'^+echo'
不是更简单吗?对于
awk
,这是一种笨拙的用法。已测试但不起作用。我注意到一件有趣的事情,当我使用
bash-x
时,我甚至不能用
grep-v
显示输出,而使用
echo“Something”grep-v
效果更好。@Caleb是的,这是一个更好的主意。我忘了那个选项:)@user359650 bash-x正在写stderr。您需要将stderr管道连接到stdout,并将其grep。我用一个例子更新了我的答案。问题是-x正在向stderr写入数据,而您只是在用管道传输stdout。只需将grep更改为:bash-x test.sh2>&1 | grep-v'+echo'
$ ./test.sh 2>&1 | grep -v '^\+ echo'