我的bash shell中有一个字符串,如:
out=$(su - user -c "someCommand -f 'string text "problemString"'")
这里的问题是它被解析为:
out=\$(su - user -c \"someCommand -f 'string text \"problemString\"'\")
我不希望"problemString"
被解析出来 - 也就是说,它需要保持原样,包括引号。我怎么能这样做?
更新:我试图通过以下方式逃离内部"
:
out=$(su - user -c "someCommand -f 'string text \"problemString\"'")
,
但是当在主机上执行命令时,它会从someCommand
:
Unknown command '\p'
更新2:
真实的例子:
OUTPUT=$(su - mysql -c "mysql --skip-column-names --raw --host=localhost --port=3306 --user=user--password=pass -e 'show variables where variable_name = \"max_connections\"'")
我通过Python中的结构传递这个bash脚本:
# probably not relevant, but just in case..
def ParseShellScripts(runPath, commands):
for i in range(len(commands)):
if commands[i].startswith('{shell}'):
# todo: add validation/logging for directory `sh` and that scripts actually exist
with open(os.path.join(runPath, 'sh', commands[i][7:]),"r") as shellFile:
commands[i] = shellFile.read()
print commands[i]
return commands
打印:
OUTPUT=$(su - mysql -c "mysql --skip-column-names --raw --host=localhost --port=3306 --user=pluto_user --password=pluto_user -e 'show variables where variable_name = \"max_connections\"'")
然后通过结构在某个远程框上执行,结果为ERROR at line 1: Unknown command '\m'.
答案 0 :(得分:0)
你可以写:
out=$(su - user -c "someCommand -f 'string text \"problemString\"'")
答案 1 :(得分:0)