我是 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')
原文由 Abdul Manaf 发布,翻译遵循 CC BY-SA 4.0 许可协议
您不能将消息作为退出代码返回,只能返回数字。在 bash 中,它可以通过
$?
访问。您也可以使用sys.argv
访问代码参数:在外壳中: