for file in $(ls); do mv "$file" "${file}_new"; done
这工作正常但是在调用这个命令之前,我想看看它实际上是什么,所以我会在前面增加一个回音
for file in $(ls); do echo mv "$file" "${file}_new"; done
然后它显示我将调用的所有命令.如果我对他们感到满意,我删除回声并执行它.
然而,当命令有点更微妙,可能包括管道或多个命令,这不再工作.当然我可以使用’所以特殊字符不被解释,但是我没有扩展参数.我也可以逃避特殊的角色,但这样会变得非常繁琐.
我的问题是,最好的方法是什么?我读过man bash关于选项-n,它是“读取命令但不执行它们”,这可能用于检查shell脚本的语法错误,这被交互式shell忽略.这正是我需要的,但是我需要一个交互式的shell.请注意,选项-x或-v没有帮助,因为它不仅会显示命令,而且还会调用它,然后可能已经太晚了.
参考Eric Blake的回应:
>
My question is why can’t such an option or be provided,A little thought would show why this will never be implemented. What
would such an option output for the following:06000
On the line for
$foo args
,there is no way to know what$foo
expands
to unless you have prevIoUsly executed (not just scanned) the
complex_command
. Therefore,there is no way to dry run what the final
results will be without running things,but running things is counter
to the goal of a dry run.That said,you might be interested in the
bashdb
project,which uses bash hooks to provide a debugger interface where you can single-step through a bash script; it’s not the same as telling you what the script would do,but it at least lets you control how much or little of the script is actually run.