带引号参数的包装脚本

发布于 2025-01-02 08:26:27 字数 509 浏览 3 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷不放纵 2025-01-09 08:26:27

您需要在 $@ 参数的实际使用周围加上引号,即

 source env.sh 
 echo "python $0.py $@"
 python $0.py "$@"

我希望这会有所帮助。

you need quotes around the actual use of the $@ parameter, i.e.

 source env.sh 
 echo "python $0.py $@"
 python $0.py "$@"

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文