如何了解如何获得在bash引用正确的? [英] How do I learn how to get quoting right in bash?

查看:116
本文介绍了如何了解如何获得在bash引用正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被规则引用,当我在写的bash脚本评估不断混淆。我知道的一些基本知识,如之间的区别''和,和``,但我仍然似乎弄错了过于频繁,并减少与尝试各种不同的方式说同样的事情试验。

I'm constantly confused by the rules for quoting and evaluating when I'm writing bash scripts. I know some of the basics, like the difference between '' and "" and ``, but I still seem to get it wrong far too often, and be reduced to experimenting with trying all sorts of different ways to say the same thing.

任何个人问题,我可以用蛮力通常工作了,但我觉得我它是如何工作的概念模型必须以某种未知的方式被打破绝望

Any individual problem I can usually work out by brute force, but I think my conceptual model of how it works must be hopelessly broken in some unknown way.

我有口齿不清的报价,EVAL,阅读,打印,语法报价系统没有问题。事实上,我写了一个小卡塔帮助人们了解了事情的原委:的 http://www.learningclojure.com/2010/11/syntax-quote-kata-for-confused.html

I have no problem with lisp's quote,eval,read,print,syntax-quote system. In fact I wrote a little kata to help people understand what was going on: http://www.learningclojure.com/2010/11/syntax-quote-kata-for-confused.html

我想,我在寻找对于bash类似的东西,(这似乎要复杂得多)。一个好的模型,或一组练习,会帮助我形成这样的模式,这将使我能够看一个复杂的shell脚本,其中的变量正在转化和评估,并打印出来,阅读,工作发生了什么事情要发生无需尝试。

I guess that I'm looking for something similar for bash, (which seems much more complicated). A good model, or a set of exercises that will help me form such a model, that will make me able to look at a complicated shell script where variables are being transformed and evaluated and printed out and read, and work out what's going to happen without having to try it.

如果做不到这一点,调试过程中,看发生了什么事在评价每一个阶段将是非常有益的好方法。

Failing that, good methods of debugging the process, and watching what's going on at each stage of the evaluation would be really helpful.

推荐答案

布鲁斯·巴内特的UNIX外壳报价教程是真棒,而Bash 常见问题解答 /的陷阱 / 分词文章都吨有用的提示。一个简短的总结:

Bruce Barnett's UNIX Shell Quote Tutorial is awesome, and the Bash FAQ/pitfalls/word splitting articles have tons of useful tips. A short summary:

无报价字符串可以包含大多数字符,但不是所有的(如新行),其中许多人(包括空格)将不得不进行转义。只要不使用他们 - 如果你爱上的诱惑,你可能会发现,人谁修改了剧本忘了,包括报价,一旦他们成为必要。

Unquoted strings can contain most characters, but not all (like newlines) , and many of them (including space) will have to be escaped. Just don't use them - If you fall for the temptation you might find that someone who modified the script forgot to include quotes once they became necessary.

单引号字符串可以包含大多数字符,包括NUL和换行符,但不是单引号,所以他们也有用的,只有简单的数值。

Single quoted strings can contain most characters, including NUL and newlines, but not single quotes, so they are also useful only for simple values.

反引号是命令。他们应该只有当你的shell不支持使用 $()。例如:

Backticks are for commands. They should only be used if your shell does not support $(). Example:

current_dir=`pwd` # BAD! Don't do this!

这是命令是不好的,因为当赋值的右手边是没有报价外壳执行分词就可以了。它往往导致难以重现bug,因为空白是很难用肉眼检查。要报价命令您的有无的使用双引号

That command is bad, because when the right hand side of an assignment is not quoted the shell performs word splitting on it. It often leads to hard-to-reproduce bugs, because whitespace is difficult to check visually. To quote commands you have to use double quotes:

current_dir="$(pwd)" # OK, but loses newlines at EOF

在换行符EOF尤其棘手。您可以添加一个字符,通过使用例如剥离它

Newlines at EOF are especially tricky. You can add a single character and strip it by using for example

# Works for some commands, but not pwd
current_dirx="$(pwd; echo x)"
current_dir="${current_dirx%x}"
printf %s "$current_dir"

,但有一个额外的困难,因为某些命令(如 PWD )将在其输出的反正末尾添加一个新行的,所以你可能必须删除,以及:

, but there's an additional difficulty because some commands (like pwd) will add a newline at the end of their output anyway, so you might have to remove that as well:

# Works for some commands, including pwd
current_dirx="$(pwd; echo x)"
current_dir="${current_dirx%$'\nx'}"
printf %s "$current_dir"

双引号可以包含的任何的字符(尝试回声-ne\\ 0|厕所-c ),但要注意,变量可以' ŧ包含NUL字符。

Double quotes can contain any character (Try echo -ne "\0" | wc -c), but note that variables can't contain the NUL character.

ANSI-C报价 可以包含任何字符的除NUL 的(尝试回声-ne $'\\ 0'|厕所-c ),并提供方便的逃生codeS,使其更容易与特殊字符的工作:

ANSI-C quotes can contain any characters except NUL (Try echo -ne $'\0' | wc -c), and provides handy escape codes to make it easier to work with special characters:

printf %s $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
printf %q $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
touch -- $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'
rm -- $'--$`!*@\a\b\E\f\r\t\v\\\'"\360\240\202\211 \n'

这篇关于如何了解如何获得在bash引用正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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