加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 显示命令而不执行它们

发布时间:2020-12-15 18:54:12 所属栏目:安全 来源:网络整理
导读:我经常以交互方式循环.我的文件,并希望对所有文件执行一个特定的操作,假设我想重命名所有文件: for file in $(ls); do mv "$file" "${file}_new"; done 这工作正常但是在调用这个命令之前,我想看看它实际上是什么,所以我会在前面增加一个回音 for file in $
我经常以交互方式循环.我的文件,并希望对所有文件执行一个特定的操作,假设我想重命名所有文件:
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没有帮助,因为它不仅会显示命令,而且还会调用它,然后可能已经太晚了.

This线程会告诉你为什么显示命令而不是执行那些(a.k.a干运行)的选项将永远不会实现为bash.

参考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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读