简体   繁体   English

将带有单引号和双引号的参数传递给Shell脚本

[英]Passing an argument with single and double quotes to shell script

We are trying to pass an argument with single and double quotes to a shell script and executing with this argument. 我们试图将带有单引号和双引号的参数传递给Shell脚本并使用该参数执行。 In echo its printing correctly but for the command we are getting as "Unterminated quoted value" 回显其打印正确,但是对于命令,我们得到的是“未终止的引用值”

Please see the script and argument passing method: 请参阅脚本和参数传递方法:

[root@geopc]/root# ./myscript.sh 'localhost:9199/jmxrmi -O java.lang:type=GarbageCollector,name="PS MarkSweep" -A CollectionCount -K duration'


#!/bin/bash
out=`/usr/lib64/nagios/plugins/check_jmx -U service:jmx:rmi:///jndi/rmi://$1`
echo $1
echo $out

After executing we are getting output as 执行后,我们得到的输出为

$1 : localhost:9199/jmxrmi -O java.lang:type=GarbageCollector,name="PS MarkSweep" -A CollectionCount -K duration $ 1:本地主机:9199 / jmxrmi -O java.lang:type = GarbageCollector,name =“ PS MarkSweep” -A CollectionCount -K duration

$out : JMX CRITICAL Unterminated quoted value $ out:JMX CRITICAL无终止报价

In Shell Script we we hard code value of $1 and then executing we are getting correct result. 在Shell Script中,我们对$ 1的值进行硬编码,然后执行,即可得到正确的结果。

We tried passing of arguments as follows: 我们尝试传递参数如下:

./myscript.sh 'localhost:9199/jmxrmi -O java.lang:type=GarbageCollector,name=\"PS MarkSweep\" -A CollectionCount -K duration'

in this case error is : JMX CRITICAL Invalid character '"' in value part of property 在这种情况下,错误是:JMX CRITICAL属性的值部分中的无效字符'“'

./myscript.sh 'localhost:9199/jmxrmi -O java.lang:type=GarbageCollector,name="\"PS MarkSweep\"" -A CollectionCount -K duration'

in this case error is JMX CRITICAL Unterminated quoted value 在这种情况下,错误是JMX CRITICAL未终止的引用值

So anyone please help me on it. 所以任何人都可以帮助我。

The problem is that you're expecting quotes and escapes in variables to be interpreted the same as quotes in your script. 问题是您期望变量中的引号和转义符与脚本中的引号解释相同。

In Java terms, this is the same as expecting: 用Java术语,这与预期相同:

String input="\"foo\", \"bar\"";
String[] parameters = { input };

to be the same as this: 与此相同:

String[] parameters = { "foo", "bar" };

The real solution in both language is to let the input be an array of elements: 两种语言的真正解决方案是让输入为元素数组:

#!/bin/bash
out=`/usr/lib64/nagios/plugins/check_jmx -U service:jmx:rmi:///jndi/rmi://"$1" "${@:2}"`
echo "$1"
echo "$out"

and then running: 然后运行:

./myscript.sh localhost:9199/jmxrmi -O java.lang:type=GarbageCollector,name="PS MarkSweep" -A CollectionCount -K duration

HOWEVER!! 然而!!

This requires discipline and understanding. 这需要纪律和理解。

You'll notice that this is not a drop-in solution: the script is now being called with multiple parameters. 您会注意到,这不是一个嵌入式解决方案:该脚本现在使用多个参数进行调用。 This is a very strict requirement. 这是一个非常严格的要求。 If this script is being called by another program, that program also has to be aware of this. 如果另一个程序正在调用此脚本,则该程序也必须意识到这一点。 It needs to be handled end-to-end. 它需要端到端处理。

If you store this in a config file for example, it'll be YOUR responsibility to serialize the string in such a way that YOU can deserialized into a list. 例如,如果将其存储在配置文件中,则您有责任以可以反序列化为列表的方式来序列化字符串。 You can't just hope that it'll work out, because it won't. 您不能只是希望它会成功,因为它不会。 Unlike Java, there will be no compiler warning when you confuse strings and lists. 与Java不同,当您混淆字符串和列表时,不会出现编译器警告。

If this is not something you want to get involved with, you can instead use eval . 如果这不是您要参与的事情,则可以使用eval This is a bad practice because of security and robustness issues, but sometimes it's Good Enough: 由于安全性和健壮性问题,这是一个不好的做法,但有时足够好:

yourcommand="/usr/lib64/nagios/plugins/check_jmx -U service:jmx:rmi:///jndi/rmi://$1"
echo "$yourcommand"  # Make sure this writes the right command
eval "$yourcommand"  # This will execute it as if you copy-pasted it

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

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