在一个shell脚本,我如何回显所有shell命令调用和展开任何变量名?

例如,给定以下一行:

ls $DIRNAME

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

ls /full/path/to/some/dir

目的是保存所有调用的shell命令及其参数的日志。是否有更好的方法来生成这样的日志?


当前回答

您还可以通过将脚本中的选定行包装在set -x和set +x中来切换此选项,例如,

#!/bin/bash
...
if [[ ! -e $OUT_FILE ]];
then
   echo "grabbing $URL"
   set -x
   curl --fail --noproxy $SERV -s -S $URL -o $OUT_FILE
   set +x
fi

其他回答

根据TLDP的Bash初学者指南:第2章。编写和调试脚本:

2.3.1. 调试整个脚本 $ bash -x script1.sh ... 现在在SourceForge上有一个成熟的Bash调试器。从3.x开始,这些调试特性在大多数现代版本的Bash中都可以使用。 2.3.2. 调试脚本的一部分 set -x #从这里激活调试 w set +x #从这里停止调试 ... 表2 - 1。set调试选项概述

    Short  | Long notation | Result
    -------+---------------+--------------------------------------------------------------
    set -f | set -o noglob | Disable file name generation using metacharacters (globbing).
    set -v | set -o verbose| Prints shell input lines as they are read.
    set -x | set -o xtrace | Print command traces before executing command.

... 或者,这些模式可以在脚本本身中指定 在第一行shell声明中添加所需的选项。 选项可以组合,就像UNIX命令通常的情况一样: # !/bin/bash十五

Set -x或Set -o xtrace展开变量并在行前打印一个小的+号。

Set -v或Set -o verbose不会在打印之前展开变量。

使用set +x和set +v关闭上述设置。

在脚本的第一行,可以输入#!/bin/sh -x(或-v)与后面脚本中的set -x(或-v)具有相同的效果。

上面的代码也适用于/bin/sh。

关于set属性和调试,请参阅bash-hackers的wiki。

$ cat shl
#!/bin/bash                                                                     

DIR=/tmp/so
ls $DIR

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

我使用一个函数来回显并运行命令:

#!/bin/bash
# Function to display commands
exe() { echo "\$ $@" ; "$@" ; }

exe echo hello world

的输出

$ echo hello world
hello world

对于更复杂的命令管道等,你可以使用eval:

#!/bin/bash
# Function to display commands
exe() { echo "\$ ${@/eval/}" ; "$@" ; }

exe eval "echo 'Hello, World!' | cut -d ' ' -f1"

的输出

$  echo 'Hello, World!' | cut -d ' ' -f1
Hello

另一种选择是在你的脚本顶部放置"-x",而不是在命令行上:

$ cat ./server
#!/bin/bash -x
ssh user@server

$ ./server
+ ssh user@server
user@server's password: ^C
$

可以使用-x选项在调试模式下执行Bash脚本。

这将回显所有命令。

bash -x example_script.sh

# Console output
+ cd /home/user
+ mv text.txt mytext.txt

您也可以在脚本中保存-x选项。只需在shebang中指定-x选项。

######## example_script.sh ###################
#!/bin/bash -x

cd /home/user
mv text.txt mytext.txt

##############################################

./example_script.sh

# Console output
+ cd /home/user
+ mv text.txt mytext.txt