我看到一个客户端执行 $bash -x
命令来查看文件是否可执行(或 ksh -x
命令等),就像 - x
在 shell 脚本的 if
语句中。
我的问题是:$bash -x
命令有什么作用?
我的解释是在当前 shell 中的新 bash shell 中启动命令,继承相同的环境变量并由相同的用户执行。
有趣的是我可以执行 $ls
但不能执行 $bash -x ls
,它给出:
(在 AIX 6 下)
/usr/bin/ls:/usr/bin/ls: 无法执行二进制文件
错误的原因对我来说是个谜 - 猜测这是由于特权,这意味着我上面的假设是不正确的。
此外,我相信 $bash ls
和 $bash -x ls
是同一件事(-x
表示“执行”)?
非常感谢任何评论。
干杯!
最佳答案
-x
选项以跟踪模式启动 BASH shell。您可以看到有关如何处理您的命令/脚本的所有详细信息。如果您的脚本没有按照您的预期执行,这是找到一些错误的好方法
而且,正如亚历克斯所说in a comment ,要在 BASH 中运行命令,您必须使用 -c
选项,例如 bash -x -c ls
。
参见 man bash
或在线手册,特别是 invoking Bash 上的部分和 the set
builtin command了解更多信息:
All of the single-character options used with the
set
builtin (see The Set Builtin) can be used as options when the shell is invoked.
-x
Print a trace of simple commands,
for
commands,case
commands,select
commands, and arithmeticfor
commands and their arguments or associated word lists after they are expanded and before they are executed. The value of thePS4
variable is expanded and the resultant value is printed before the command and its expanded arguments.
关于bash -x 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107124/