python - 使用字符串和 python 列表参数从终端运行 shell 脚本

标签 python shell

我有一个 shell 脚本,它从我创建的 python 文件中调用函数。该函数需要两个参数,一个字符串和一个字符串列表。我正在尝试弄清楚如何从控制台调用该脚本。我的脚本是:

 #!/bin/sh
 cd $1
 python -c "from my_file import my_function; my_function( \"$2\" )"

第一个参数是 my_file.py 的路径,第二个参数是 python 字符串列表。如何从终端调用脚本?

如果我从 python 文件调用它,我的脚本就可以工作(例如输入两个字符串):

arguments = ["./script.sh", path, args1]
ret_val = subprocess.Popen(arguments, stdout=subprocess.PIPE)

最佳答案

也许你可以将它作为命令行参数传递给 python。

#!/bin/sh
cd $1
python -c "from my_file import my_function; import sys; my_function(sys.argv[1])" $2

关于python - 使用字符串和 python 列表参数从终端运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50944987/

相关文章:

python - 检查Python中的元素是否乱序

python - 有没有简单的方法来旋转矩阵/数组的值?

shell - 如何在ansible shell任务中正确使用jq

linux - 如何将存在于多个子文件夹中的多个目录中的文件复制到两层以上?

bash - 测试是否在 bash 中设置了 env 变量

python - 为什么 'itsdangerous' 模块返回签名文本?

python - MySQLdb Python-如果不存在则使用CREATE TABLE时仍然出现错误

regex - 带有多个参数的 pgrep -f

linux - 从另一个 bash 脚本中有选择地打开和关闭 bash 脚本

python - 如何有效地将 scipy 稀疏数组和 numpy 数组分割成较小的 N 个不等 block ?