Bash字符串解析,被内部字符串扭曲
我的bash shell中有一个字符串,如:Bash字符串解析,被内部字符串扭曲,bash,shell,Bash,Shell,我的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“someComma
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
返回一个错误:
未知命令'\p'
更新2:
实例:
OUTPUT=$(su-mysql-c“mysql--skip列名--raw--host=localhost--port=3306--user=user--password=pass-e”显示变量名=\“max\u connections\”)
我通过Python中的fabric传递此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列名--raw--host=localhost--port=3306--user=pluto\u user--password=pluto\u user-e'显示变量名=\“max\u connections\”)
然后通过结构在某个远程框上执行,这会导致第1行出现错误:未知命令“\m”。
您可以编写:
out=$(su - user -c "someCommand -f 'string text \"problemString\"'")
只需使用单引号。单引号中的字符串不会被解析或解释。例如:
echo 'a"b'
产出:
a"b
因为没有解析发生
仅供参考:.您是如何获取此字符串的?您是如何使用它的?
someCommand-f'string text\'problemString\'“
您位于双引号内,因此需要转义双引号。围绕它的单引号不会从$()
context。您能否给出一个具体的例子,它对您不起作用,并且是可复制的(即使用无处不在的“someCommand”)?我已经添加了更多的细节,谢谢。在这样做的时候,我从someCommand
收到一条消息:未知命令'\p'。
所以它好像还在删除“
,保留“那么你实际做的事情与你要求的不符。”。这都是文字字符串,对吗?如果其中任何一个在变量中,这会改变一切。是的,它们都不在变量中。。。我将用更多细节更新我的问题。