gpt4 book ai didi

python - 从shell脚本向python程序插入参数

转载 作者:太空宇宙 更新时间:2023-11-04 10:36:13 26 4
gpt4 key购买 nike

我需要从 shell 脚本执行我的 python 程序,这样我的 python 程序中的打印命令就会暴露出来,以便从另一个程序读取。 shell 脚本:

#!/bin/bash

if [ $# -lt 1 ] || [ $# -gt 2 ];then
echo "Usage: $0 APK <duration in seconds>"
exit 1;
fi

printf "$(python '/root/Desktop/DroidBox_4.1.1/scripts/droidbox.py' $1 $2 -c 'import sys; exec sys.stdin.read()')"

我的 python 程序应该获取参数 $1 和 $2,但它不将这些参数识别为参数,而是将 -c 和它后面的命令作为参数。

对于这样的答案:在我的其他项目中获取流程输入流对我不起作用。唯一可行的方法是使用 -c 选项和命令“exec sys.stdin.read()”。

谢谢。

最佳答案

它应该按照您编写的方式工作得很好。在精简版中,这是我得到的:

(bash) test.sh 脚本:

#!/bin/bash    
python /tmp/test.py $1 $2

(python)test.py 脚本:

import sys
print "in python"
print sys.argv

最后是一个 shell session :

smassey@hacklabs:/tmp $ ./test.sh hello world
in python
['/tmp/test.py', 'hello', 'world']

如您所见,bash 脚本调用将值打印到 stdout 的 python 脚本,因此它们与指向 stdout 的任何其他内容一样公开:

smassey@hacklabs:/tmp $ ./test.sh hello world | grep python | tr 'a-z' 'A-Z'
IN PYTHON

关于python - 从shell脚本向python程序插入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com