show命令不执行他们 [英] Show commands without executing them

查看:122
本文介绍了show命令不执行他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我常常交互遍历例如我的文件,并希望对他们都进行具体的操作,让我们说,我想重命名的所有文件:

I often interactively loop over e.g. my files and want to perform a specific operation on all of them, let's say I'd like to rename all files:

for file in $(ls); do mv "$file" "${file}_new"; done

这工作正常。但是,调用此命令之前,我想看看它实际做,所以我会在前面加一个回声

This works fine. But before invoking this command, I'd like to see what it actually does, so I would add an echo in front

for file in $(ls); do echo mv "$file" "${file}_new"; done

这则表明我,它会调用所有命令。如果我很高兴与他们,我删除回声并执行它。

然而,当命令是多一点微妙的,也许包括管道或多个命令,这不工作了。当然,我可以使用 这样的特殊字符,不要跨preTED,但当时我没有参数扩展。我还可以逃避特殊字符,但这样会变得非常乏味的。

However, when the commands are a bit more subtle maybe including pipes or more than one command, this doesn't work anymore. Of course I could use ' so the special characters don't get interpreted, but then I don't have parameter expansion. I could also escape the special characters, but this would get very tedious.

我的问题是,什么是做到这一点的最好方法是什么?我读过的男人庆典哪个不读取命令,但不执行的选项 -n 。这可以用来检查一个shell脚本语法错误。这是通过交互shell忽略。这正是我需要的,但我需要它的一个交互的shell。注意选项 -x -v 没有帮助,因为它不会只显示命令,也调用它,那么它可能是太晚了。

My question is, what's the best way to do this? I've read in man bash about the option -n, which does "Read commands but do not execute them. This may be used to check a shell script for syntax errors. This is ignored by interactive shells." This is exactly what I need, but I need it for an interactive shell. Note that the options -x or -v do not help, as it will not only show the command, but also invoke it and then it might be too late already.

推荐答案

线程会告诉你为什么选项来显示的命令,而不是执行这些(又名试运行的)将永远不会被庆典实施。

This thread would tell you why the option to show commands instead of executing those (a.k.a dry run) would never be implemented for bash.

请参阅从埃里克·布雷克响应:

> 我的问题是,为什么不能提供这样的选项,或

> 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:

if complex_command; then
  foo=command1
else
  foo=command2
fi
$foo args

在对 $ foo的ARGS 行了,没有办法知道 $ foo的扩张
  到除非你有previously执行(不只是扫描)的
   complex_command 。因此,没有办法进行干燥运行哪些最终
  结果将是不运行的东西,但运行的东西是反
  到试运行的目标。

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.

这是说,你可能有兴趣在 bashdb 的项目,它使用
  bash的挂钩提供一个调试器界面,用户可以单步
  通过bash脚本;这是不一样的告诉你什么
  脚本会做,但它至少可以让你控制多少或少
  的脚本实际运行。

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.

这篇关于show命令不执行他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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