BuildForge获取Linux shell命令执行的返回值

本文深入探讨了在BuildForge环境中如何通过多种方法获取LinuxShell命令执行后的返回值,包括使用四重美元符号绕过预解析、设置环境变量以及在脚本中检查命令执行状态等技巧。同时,文章提供了实例代码,帮助开发者更好地理解和应用这些方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在buildforge中如何获取Linux shell运行的return code

例子:
perl test.pl -option
chmod 777 some_file
ln some_file current_link
以以上三个命令作为例子,如果这三个命令不加任何处理写在build forge的step中,build forge总是以最后一跳命令的返回值最为最后的返回值。
由于buildforge的servr可能安装不同的bash,以hash/ksh为例:


有两种方法来获取return code
1Add multiple dollar signs($) to a variable so to get past the various preparsers.  We have found that in most cases it takes four(4) $$$$?.

perl test.pl -option
if [ 0 -eq $$$$? ] ; then
 echo "(I)script.pl was successful"
else
 echo "(E)test.pl failed"
exit 2
fi

chmod 777 some_file
if [ 0 -eq $$$$? ] ; then
 echo "(I)chmod was successful"
else
 echo "(E)chmod failed"
exit 2
fi

ln some_file current_link
# Since this is the last command you would not have to check the return code as BuildForge will, but we will anyway
if [ 0 -eq $$$$? ] ; then
 echo "(I)Link was successful"
else
 echo "(E)Link failed"
exit 2
fi


2Set the _NO_PREPARSE_COMMAND variable to 1 for a project or a step
关于:
_NO_PREPARSE_COMMAND
    

The system normally attempts to resolve the values of environment variables before sending commands to agents. When the _NO_PREPARSE_COMMAND variable is defined (with any value), the system sends variables to agents without resolving them. Use this variable to ensure that your operating system shell handles the variables.

例子:
.tset env "_NO_PREPARSE_COMMAND=1"
perl test.pl -option
if [ 0 -eq $? ] ; then
 echo "(I)test.pl was successful"
else
 echo "(E)test.pl failed"
exit 2
fi

chmod 777 some_file
if [ 0 -eq $? ] ; then
 echo "(I)chmod was successful"
else
 echo "(E)chmod failed"
exit 2
fi

ln some_file current_link
# Since this is the last command you would not have to check the return code as BuildForge will, but we will anyway
if [ 0 -eq $? ] ; then
 echo "(I)Link was successful"
else
 echo "(E)Link failed"
exit 2
fi


补充一些相关的知识:
http://publib.boulder.ibm.com/infocenter/bldforge/v7r1m2/index.jsp?topic=/com.ibm.rational.buildforge.doc/topics/env_var_interpretation.html

首先:如何在step中定义变量
支持unix风格和windows风格的变量语法
windows风格的如:
set a=100
echo %a%
unix或者linux风格的如:
f=200
echo $f


.tset命令:用来临时改变设置,仅仅在当前步骤中生效。语法.tset env  "<VariableName>=<DesiredValue>" [...]

However, threading can affect this behavior. Example:

   1. A step is threaded and it specifies an Inline.
   2. The first step of the Inline is also threaded.

注意以上两句,不过我还没来及研究

.bset:范围更大,对当前job有效
.bset env "VAR1=VALUE1"
.bset env "VAR2=$VAR1"
如果对一个变量有多个.bset,总是以最后一个运行的有效


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值