我是Python新手。我正在创建一个返回字符串“hello world”的 Python 脚本。我正在创建一个 shell 脚本。我正在添加从 shell 到 Python 脚本的调用。
- 我需要将参数从 shell 传递到 Python。
- 我需要在 shell 脚本中打印从 Python 返回的值。
这是我的代码:
shellscript1.sh
#!/bin/bash
# script for testing
clear
echo "............script started............"
sleep 1
python python/pythonScript1.py
exit
pythonScript1.py
#!/usr/bin/python
import sys
print "Starting python script!"
try:
sys.exit('helloWorld1')
except:
sys.exit('helloWorld2')
您不能将消息作为退出代码返回,只能返回数字。在 bash 中可以通过以下方式访问$?
。您也可以使用sys.argv
访问代码参数:
import sys
if sys.argv[1]=='hi':
print 'Salaam'
sys.exit(0)
在外壳中:
#!/bin/bash
# script for tesing
clear
echo "............script started............"
sleep 1
result=`python python/pythonScript1.py "hi"`
if [ "$result" == "Salaam" ]; then
echo "script return correct response"
fi
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)