从bash脚本传递参数到python解释器

时间:2022-08-21 17:09:01

Sorry this is a very newbie question, but I just can't seem to get it to work.

对不起,这是一个非常新手的问题,但我似乎无法让它工作。

in my bash script, I have

在我的bash脚本中,我有

python=/path/to/python
script=$1
exec $python $script "$@"

How would I pass an argument, say -O to the python interpreter? I have tried:

我如何传递一个参数,比如说-O到python解释器?我努力了:

exec $python -O $script "$@"

exec $ python -O $ script“$ @”

And have tried changing python variable to "/path/to/python -O", as well as passing -O to the script, but every time i do any of these three, I get import errors for modules that succeed when I remove the -O.

并尝试将python变量更改为“/ path / to / python -O”,以及将-O传递给脚本,但每次我执行这三个中的任何一个时,我会在删除时取得成功的模块导入错误-O。

So my question is how to tell the python interpreter to run with -O argument from a bash script?

所以我的问题是如何告诉python解释器使用bash脚本中的-O参数运行?

Thanks.

谢谢。

1 个解决方案

#1


1  

You should shift your positional parameters to the left by 1 to exclude your script which is in the first arguments from being included to the arguments for python.

您应该将位置参数向左移动1,以排除第一个参数中的脚本被包含到python的参数中。

#!/bin/sh
python=/path/to/python
script=$1; shift
exec "$python" -O "$script" "$@"

Then run the script as bash script.sh your_python_script arg1 arg2 ... or sh script.sh your_python_script arg1 arg2 ....

然后将脚本运行为bash script.sh your_python_script arg1 arg2 ...或者sh script.sh your_python_script arg1 arg2 ....

#1


1  

You should shift your positional parameters to the left by 1 to exclude your script which is in the first arguments from being included to the arguments for python.

您应该将位置参数向左移动1,以排除第一个参数中的脚本被包含到python的参数中。

#!/bin/sh
python=/path/to/python
script=$1; shift
exec "$python" -O "$script" "$@"

Then run the script as bash script.sh your_python_script arg1 arg2 ... or sh script.sh your_python_script arg1 arg2 ....

然后将脚本运行为bash script.sh your_python_script arg1 arg2 ...或者sh script.sh your_python_script arg1 arg2 ....