在bash脚本中正确转义Json命令行选项 [英] Properly escaping Json command line option in bash script
本文介绍了在bash脚本中正确转义Json命令行选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有接受JSON作为参数的实用程序.如何正确转义json传递给实用程序?示例:
I have utility which accept JSON as argument. How to escape json correctly to pass to the utility? Example:
ip="127.0.0.1"
action='{\"server_ip\":\"$ip\",\"action\":\"stop\"}'
./fix-utility -e $action
但是JSON无法正确转义.
But JSON is not getting escaped correctly.
推荐答案
如果您想对例如在字符串中的 $ ip
中,您需要使用双引号.值中的双引号需要反斜杠转义.
If you want interpolation of e.g. $ip
inside the string, you need to use double quotes. Double quotes in the value need backslash escaping.
ip="127.0.0.1"
action="{\"server_ip\":\"$ip\",\"action\":\"stop\"}"
./fix-utility -e "$action"
实际上,我建议不要将操作存储在变量中,除非您的示例省略了一些必要的关键操作.
Actually, I would advise against storing the action in a variable, unless your example omits something crucial which makes it necessary.
这篇关于在bash脚本中正确转义Json命令行选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文