Skip to main content
 首页 » 编程设计

python之使用 Shell 脚本从 python 获取返回值

2025年02月15日12bhlsheji

我知道已经提出并回答了类似的问题。
但是,我的问题涉及的问题和实现类型略有不同:

情况是:
我需要构建一个 shell 脚本,它将捕获 python (2.7) 脚本的返回值,然后将其用于 jenkins 作业。
在 python 脚本中,我必须读取一个 SVN check out 文本文件并返回文件的值,更新文件的值,然后提交。

我已经实现了以下代码:

swdl_number_receiver.py 
 
import posixpath 
import os 
import sys 
import subprocess 
 
SVN_VERSION_DIR_PATH = 'data' 
SWDL_FILE_NAME = "swdl_number.txt" 
 
# Exit codes 
class ExitCode: 
   """ Exit code Enumeration """ 
    SUCCESS = 0 
    FAILURE = 1 
 
# get SWDL number from the file 
def getSWDLNumber(): 
work_dir = posixpath.join(os.getcwd(),SVN_VERSION_DIR_PATH) 
version_number = None 
new_version_number = None 
 
# Read the File 
os.chdir(work_dir) 
with open(SWDL_FILE_NAME, "r") as input_file: 
    for line in input_file: 
        version_number = str.strip(line) 
        if version_number is None: 
            print "---- SWDL NUmber is empty existing the code ....... " 
            sys.exit(ExitCode.FAILURE) 
        else: 
            print "++++ The found SWDL number is : " + version_number 
            new_version_number = int(version_number) + 1 
            print "++++ The New SWDL Number is  : " + str(new_version_number) 
 
print "++++ Writing the new SWDL Number from " + str(version_number) + " to " + str(new_version_number) 
#replace the file with new content 
os.remove(SWDL_FILE_NAME) 
with open(SWDL_FILE_NAME, 'wb') as output: 
    output.write(str(new_version_number).rstrip('\n')) 
 
checkin_message = '"Updated SWDL Number to : ' + str(new_version_number) + '"' 
try: 
    subprocess.call(['svn', 'commit', '-m ' + checkin_message, SWDL_FILE_NAME, '--non-interactive']) 
except: 
    sys.exit(ExitCode.FAILURE) 
 
return new_version_number 
 
 
if __name__ == "__main__": 
    print getSWDLNumber() 

在我的 shell 脚本中,我正在做:

outputString=$(python swdl_number_receiver.py) 
echo $outputString 

swdl_number.txt 仅包含一行 5 位数字。

我的问题: 当我注释掉所有“打印”行和 SVN 提交的 try 和 catch 时,我只得到 new_version_number 作为返回值。
换句话说,如果我取消注释所有带有打印语句的行和 SVN 提交的 try 和 catch block ,那么在 $output_string 的 shell 脚本上我得到每个打印语句和提交消息以及“new_version_number ”。如果我注释掉所有打印语句并 try catch block ,那么我将获得所需的“new_version_number”。

我的代码应该如何修改才能获得唯一需要的“new_version_number”返回值,并显示所有打印行?

请您参考如下方法:

outputString=$(python swdl_number_receiver.py) 

构造捕获发送到 stdout 的文本。如果您希望您的脚本将信息打印到终端,那么您可以将它发送到 stderr。这是一个简单的演示。

qtest.py

import sys 
 
print >>sys.stderr, "this is sent to stderr" 
print "this is sent to stdout" 

在 Bash 中:

$ outputString=$(python qtest.py);echo "ok";echo "$outputString" 

输出

this is sent to stderr 
ok 
this is sent to stdout 


这是适用于 Python 2 和 Python 3 的 qtest.py 版本:

from __future__ import print_function 
import sys 
 
print("this is sent to stderr", file=sys.stderr) 
print("this is sent to stdout") 

如果您还想在变量中捕获发送到 stderr 的输出,一种不更改“qtest.py”的方法是重定向 stderr在 Bash 中写入文件:

$ outputString=$(python qtest.py 2>qtemp);echo "ok";stderrString=$(<qtemp);echo "STDOUT: $outputString";echo "STDERR: $stderrString" 
ok 
STDOUT: this is sent to stdout 
STDERR: this is sent to stderr 

更好的方法是用 Python 直接写入文件:

qtest.py

from __future__ import print_function 
 
with open("qtemp", "w") as f: 
    print("this is sent to qtemp", file=f) 
    print("this is sent to stdout") 

狂欢

$ outputString=$(python qtest.py);echo "ok";qtempString=$(<qtemp);echo "STDOUT: $outputString";echo "qtemp: $qtempString" 
ok 
STDOUT: this is sent to stdout 
qtemp: this is sent to qtemp