带引号参数的包装脚本
我有一些 python 脚本,由于各种原因,我在它们周围有 shell 脚本包装器:
#!/bin/sh
source env.sh
python $0.py $@
这工作得很好,除非需要引用参数。当然,在这种情况下,包装器会吃掉引号,并将未加引号的版本提供给 python 脚本。所以,我的第一个问题是“我怎样才能让她不吃引号?”
但是,即使我反斜杠引号,它也不起作用。我打印出我要调用的整个命令:
source env.sh
echo "python $0.py $@"
python $0.py $@
如果我用 foo \"ab c" 调用它,它会输出 python foo.py "ab c" 但是,当调用 foo.py 时,它仍然会获取 foo.py abc
如果我只是复制并粘贴输出并运行它,它就会运行良好。
谁能告诉我为什么脚本实际执行会失败,但在命令行上会成功?
谢谢。
I have a few python scripts, and for various reasons, I have shell script wrapper around them:
#!/bin/sh
source env.sh
python $0.py $@
This works fine, unless the arguments needs to be quoted. In this case, of course, the wrapper, eats the quotes, and gives the un-quoted version to the python script. So, my first question is "How can I get sh to not eat the quotes?"
However, even if I back-slash the quotes, it doesn't work. I print out the entire command I'm about to call:
source env.sh
echo "python $0.py $@"
python $0.py $@
If I call it with foo \"a b c" it outputs
python foo.py "a b c"
However, when foo.py gets called, it still GETS foo.py a b c
If I just copy and paste the output, and run it, it runs fine.
Can anyone tell me why the actual execution would fail from the script, but succeed on the command line?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
$@
参数的实际使用周围加上引号,即我希望这会有所帮助。
you need quotes around the actual use of the
$@
parameter, i.e.I hope this helps.