从python脚本返回值到shell脚本 [英] return value from python script to shell script

查看:121
本文介绍了从python脚本返回值到shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python新手.我正在创建返回字符串"hello world"的Python脚本.我正在创建一个shell脚本.我正在将shell中的调用添加到Python脚本中.

I am new in Python. I am creating a Python script that returns a string "hello world." And I am creating a shell script. I am adding a call from the shell to a Python script.

  1. 我需要将参数从外壳传递给Python.
  2. 我需要在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 来访问代码参数:

You can't return message as exit code, only numbers. In bash it can accessible via $?. Also you can use sys.argv to access code parameters:

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

这篇关于从python脚本返回值到shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆