如何在执行外壳命令时回显它们 [英] How to echo shell commands as they are executed

查看:108
本文介绍了如何在执行外壳命令时回显它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell脚本中,如何回显所有被调用的shell命令并扩展任何变量名?

In a shell script, how do I echo all shell commands called and expand any variable names?

例如,给定以下行:

ls $DIRNAME

我希望脚本运行命令并显示以下内容

I would like the script to run the command and display the following

ls /full/path/to/some/dir

目的是保存所有被调用的shell命令及其参数的日志.也许有更好的方法来生成此类日志?

The purpose is to save a log of all shell commands called and their arguments. Is there perhaps a better way of generating such a log?

推荐答案

set -xset -o xtrace扩展变量并在该行之前显示一个+号.

set -x or set -o xtrace expands variables and prints a little + sign before the line.

set -vset -o verbose在打印之前不会扩展变量.

set -v or set -o verbose does not expand the variables before printing.

使用set +xset +v关闭以上设置.

在脚本的第一行,可以在脚本的后面放置#!/bin/sh -x(或-v),使其具有与set -x(或-v)相同的效果.

On the first line of the script, one can put #!/bin/sh -x (or -v) to have the same effect as set -x (or -v) later in the script.

以上内容也可用于/bin/sh.

请参见 set属性上的bash-hackers Wiki. ,以及调试.

$ cat shl
#!/bin/bash                                                                     

DIR=/tmp/so
ls $DIR

$ bash -x shl 
+ DIR=/tmp/so
+ ls /tmp/so
$

这篇关于如何在执行外壳命令时回显它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆