简体   繁体   English

Shell Scripting:使用\\ $引用变量

[英]Shell Scripting: Quotes Around Variable with \$

I've just started learning about shell scripting and have been trying to workout what's going on in this script: http://dev.cloudtrax.com/wiki/ng-cs-ip-logging 我刚刚开始学习shell脚本,并且一直在努力研究这个脚本中正在发生的事情: http//dev.cloudtrax.com/wiki/ng-cs-ip-logging

Specifically, I can't wrap my head around a couple of lines that use "\\$foo" for example: 具体来说,我无法绕过使用"\\$foo"的几行代码:

[ -z "\\$plug_event" ] && return

Everything I've read and learned about shell scripting has me believing that "\\$plug_event" would evaluate as a string whose value is $plug_event . 我读过和学习shell脚本的所有内容都让我相信"\\$plug_event"会将其评估为值为$plug_event的字符串。 This would mean that the above test would always return a 1 (ie the test was false), right? 这意味着上面的测试总会返回1 (即测试是假的),对吧? If so, what's the point? 如果是这样,重点是什么?

I've found plenty on quotes around variables but so far I haven't been able to find a single example of this kind of usage. 我已经在变量的引号上找到了很多,但到目前为止我还没有找到这种用法的单一例子。 Is it just a typo? 这只是一个错字吗? Unfortunately I'm nowhere near experienced enough to tell the difference yet. 不幸的是,我还没有足够的经验告诉它们差异。

All help is much appreciated, and a link to a relevant document would certainly suffice. 非常感谢所有帮助,并且链接到相关文档肯定就足够了。

Cheers, Kyle 干杯,凯尔

The reason for escaping all the $ s is that those lines are part of here-docs 转义所有$ s的原因是这些行是here-docs的一部分

The command cat > /etc/ip_logging.sh << EOF will output all of following text into /etc/ip_logging.sh until it hits EOF , and not to evaluate the variables in the current script, the $ has to be escaped. 命令cat > /etc/ip_logging.sh << EOF会将以下所有文本输出到/etc/ip_logging.sh直到它达到EOF ,而不是评估当前脚本中的变量, $必须被转义。

Alternatively, and to make the code easier to read, putting the terminating string in single quotes will disable variable substitution in the heredoc: 或者,为了使代码更容易阅读,将终止字符串放在单引号中将禁用heredoc中的变量替换:

cat > /etc/ip_logging.sh << 'EOF'
    [ -z "$plug_event" ]
    #other stuff
EOF

will have same result, sans the escaped $ and other shell special characters 将有相同的结果,没有转义的$和其他shell特殊字符

The test: 考试:

[ -z "\$plug_event" ]

is pointless. 毫无意义。 The string is never zero length; 字符串永远不会为零长度; the return after it is never executed. 从未执行过后的返回。

Whoever wrote that code did not understand what they were doing ... unless there are extenuating circumstances such as it is part of a here-doc which is then treated specially. 编写该代码的人不明白他们在做什么......除非有一些情有可原的情况,例如它是here-doc的一部分,然后进行特殊处理。

But, standing on its own, the statement is pointless. 但是,站在自己的立场上,这句话毫无意义。

...look at the code... ......看看代码......

# create iptables script on the fly
cat > /etc/ip_logging.sh << EOF
#!/bin/sh

. /etc/functions.sh

install_rule() {
        config_get plug_event "\$1" plug_event

        [ -z "\$plug_event" ] && return

        pub_ip=\$(uci get dhcp.pub.ipaddr)
        pub_mask=\$(uci get dhcp.pub.netmask)

        priv_ip=\$(uci get dhcp.priv.ipaddr)
        priv_mask=\$(uci get dhcp.priv.netmask)

        iptables -I POSTROUTING -t nat -o br-\$1 -s \$pub_ip/\$pub_mask -j LOG --log-level debug --log-prefix "iplog: "
        iptables -I POSTROUTING -t nat -o br-\$1 -s \$priv_ip/\$priv_mask -j LOG --log-level debug --log-prefix "iplog: "
}

config_load network
config_foreach install_rule interface
EOF

Someone did know more or less what they are up to; 有人确实或多或少知道他们要做什么; they are writing a script in a here-doc and need the parameters expanded when the script that's being generated is executed, not when it is created. 他们正在here-doc中编写脚本,并且需要在执行生成的脚本时扩展参数,而不是在创建脚本时。 They could have simplified life by using: 他们可以通过以下方式简化生活:

# create iptables script on the fly
cat > /etc/ip_logging.sh << 'EOF'

The quotes around the end marker mean that no expansions are done in the here-doc, so all the backslashes could go. 结束标记周围的引号意味着在here-doc中没有进行扩展,因此所有反斜杠都可以进行。

The bash manual is your friend. bash 手册是你的朋友。 Shell parameter expansion is one relevant section, but it covers actual expansions, not suppressed expansions. Shell参数扩展是一个相关部分,但它涵盖了实际扩展,而不是抑制扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM