我知道已经提出并回答了类似的问题。
但是,我的问题涉及的问题和实施方式略有不同:
情况是:
我需要构建一个shell脚本,它将捕获python(2.7)脚本的返回值,然后将其用于jenkins作业。
在python脚本中,我必须读取SVN签出文本文件并返回文件的值,更新文件的值并提交它。
我已经实现了以下代码:
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位数字。
我的问题:
当我注释掉所有“print”行以及SVN提交的try和catch时,我只得到new_version_number作为返回值。
换句话说,如果我取消注释所有带有print语句的行和用于SVN提交的try和catch块,那么在shell脚本上作为$ output_string我获取每个print语句和提交消息以及“new_version_number” ”。如果我注释掉所有的print语句并尝试捕获块,那么我得到了所需的“new_version_number”。
如何修改我的代码以获得唯一需要的“new_version_number”返回,并显示所有打印行?
答案 0 :(得分:1)
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
from __future__ import print_function
import sys
print("this is sent to stderr", file=sys.stderr)
print("this is sent to stdout")
如果您还希望捕获变量中发送到stderr
的输出,那么在不更改&#34; qtest.py&#34;的情况下执行此操作的一种方法是将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")
<强>的bash 强>
$ 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
答案 1 :(得分:0)
好吧,你可以在python和bash之间共享你的version file
。根据python脚本的退出代码,您可以决定是否获得有效的新版本。
Shell脚本:
# Version file name
SWDL_FILE_NAME="x.txt"
export SWDL_FILE_NAME
python swdl_number_receiver.py
# Exit code of python script
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ] ; then
# If scripts exits successfully get version
NEW_VERSION=$(cat $SWDL_FILE_NAME)
echo "New version is $NEW_VERSION"
exit 0
else
# No new version
echo "Failed to get new version"
exit 1
fi
最小的Python文件:
import sys
import os
SWDL_FILE_NAME=os.environ['SWDL_FILE_NAME']
with open(SWDL_FILE_NAME, 'w') as fp:
fp.write("111")
sys.exit(0)
<强>输出:强>
$ ./myscript.sh
New version is 111