我有一个 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/