我需要从shell脚本执行我的python程序,以便我的python程序中的print命令可以从另一个程序中读取。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()‘。
谢谢。
发布于 2016-05-23 22:09:44
就像你写的那样,它应该工作得很好。下面是我得到的精简版本:
(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脚本调用python脚本,该脚本将值打印到stdout,因此它们与定向到stdout的任何其他值一样公开
smassey@hacklabs:/tmp $ ./test.sh hello world | grep python | tr 'a-z' 'A-Z'
IN PYTHON发布于 2016-05-23 22:34:16
argparse模块也非常有用。这允许您向您的程序添加自定义标志(从用户的角度来看,这也使其使用起来更方便)。
https://stackoverflow.com/questions/37392926
复制相似问题