我需要一个方法来运行python脚本文件,如果脚本失败并带有未处理的异常,python应该以非零退出代码退出.我的第一次尝试是这样的:
import sys
if __name__ == '__main__':
try:
import <unknown script>
except:
sys.exit(-1)
Run Code Online (Sandbox Code Playgroud)
但由于__main__经常使用的防护,它打破了很多脚本.有关如何正确执行此操作的任何建议?
在我的shell脚本中,我运行此命令:
python script.py
Run Code Online (Sandbox Code Playgroud)
我想知道,作为一个两部分问题:
我如何编程我的python脚本将状态传递回运行它的shell脚本,具体取决于python脚本中发生的情况.例如,如果python脚本出现问题,则退出时将代码1发送回shell.
如何让我的shell脚本读取python的退出代码并退出以获取错误?例如,除了0之外的任何状态代码然后退出.
我做了一个简单的脚本:
$ more test.bash
#!/bin/bash
echo test
exit 1
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,退出状态应为1
$ /tmp/test.bash
echo $?
1
Run Code Online (Sandbox Code Playgroud)
但是当我按照以下方式运行时
/tmp/test.bash | tr -d '\r' 1>>$LOG 2>>$LOG
echo $?
0
Run Code Online (Sandbox Code Playgroud)
退出状态为0,(不符合预期1)
似乎退出状态来自tr命令.但我想要的是从脚本中获取退出状态 - test.bash.
为了从脚本中获取正确的退出状态,而不是从管道后面的命令中获取正确的退出状态,我需要在语法中添加/更改什么?
我现在正在使用Bash进行编程,set -e因为在程序失败时继续执行脚本几乎不是想要的行为.
如果是我使用,||true如果我不需要退出代码.
如果我需要退出代码,我将执行包装如下:
set +e
call_I_need_the_exit_code with few arguments
RV="$?"
set -e
# use "$RV" somewhat
Run Code Online (Sandbox Code Playgroud)
但是,它很冗长,我很少切换set +e和set -e引入恼人的错误.
有没有办法创建一个执行命令的函数,并为退出代码设置一个已知变量?
像这样的东西(伪代码):
safe_call( call_I_need_the_exit_code(with, few, arguments) )
# use "$RV" somewhat
Run Code Online (Sandbox Code Playgroud)
其中safe_call基本上是前一段代码.它会使我的代码更容易编写和阅读......
在一个相关的问题中,我问到哪里可以找到C函数的文档"等待".这是尝试找出commands.getstatusoutput()模块的返回码.Stackoverflow通过,但文档没有帮助.这就是困扰我的:
#!/usr/bin/python
import commands
goodcommand = 'ls /'
badcommand = 'ls /fail'
status, output = commands.getstatusoutput(goodcommand)
print('Good command reported status of %s' % status)
status, output = commands.getstatusoutput(badcommand)
print('Bad command reported status of %s' % status)
Run Code Online (Sandbox Code Playgroud)
在OS X(Leopard)上运行时,我得到以下输出:(符合文档.)
$ python waitest.py
Good command reported status of 0
Bad command reported status of 256
Run Code Online (Sandbox Code Playgroud)
在OS X上,执行"ls/fail; echo $?" 得到以下输出:
$ ls /fail ; echo $?
ls: /fail: No such file or directory
1
Run Code Online (Sandbox Code Playgroud)
在Linux(Ubuntu Hardy)上运行时,我得到以下输出:
$ python waitest.py
Good …Run Code Online (Sandbox Code Playgroud) 我想为我的安装设置退出代码,这样我就知道为什么安装被中止了.我正在使用Inno Setup.
我不知道为什么我会收到这个错误.我相信我拥有所有正确的配置文件等.我刚刚搬到新电脑,但我也带了私钥匙链开发人员密钥.我让我的开发人员文件与Dropbox同步,所以我不需要移动Xcode项目.这是错误:
CodeSign "/Users/michaellindahl/Library/Developer/Xcode/DerivedData/Fraction_Calculator-cgirhuuvywfnsyenisucsuauquoz/Build/Products/Debug-iphoneos/Fraction Calculator Pro.app"
cd "/Users/michaellindahl/Dropbox/Xcode/lindahl studios/FractionCalculator"
setenv CODESIGN_ALLOCATE /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_allocate
setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/usr/bin/codesign --force --sign "iPhone Developer: Michael Lindahl (MXXX0X0XXB)" "--resource-rules=/Users/michaellindahl/Library/Developer/Xcode/DerivedData/Fraction_Calculator-cgirhuuvywfnsyenisucsuauquoz/Build/Products/Debug-iphoneos/Fraction Calculator Pro.app/ResourceRules.plist" --entitlements "/Users/michaellindahl/Library/Developer/Xcode/DerivedData/Fraction_Calculator-cgirhuuvywfnsyenisucsuauquoz/Build/Intermediates/Fraction Calculator.build/Debug-iphoneos/Fraction Calculator Pro.build/Fraction Calculator Pro.xcent" "/Users/michaellindahl/Library/Developer/Xcode/DerivedData/Fraction_Calculator-cgirhuuvywfnsyenisucsuauquoz/Build/Products/Debug-iphoneos/Fraction Calculator Pro.app"
CSSM_SignData returned: 8001094A
/Users/username/Library/Developer/Xcode/DerivedData/Fraction_Calculator-cgirhuuvywfnsyenisucsuauquoz/Build/Products/Debug-iphoneos/Fraction Calculator Pro.app: unknown error -2070=fffffffffffff7ea
Command /usr/bin/codesign failed with exit code 1
Run Code Online (Sandbox Code Playgroud)
感谢您的见解.
我有一个脚本,检查以下功能的退出状态:
function is_git_repository {
git branch &> /dev/null
}
Run Code Online (Sandbox Code Playgroud)
0如果您使用的是git仓库,那么会返回,128如果您不是.
我测试返回值是否有问题0; 以下按预期工作:
if is_git_repository ; then
echo you are in a git repo
else
echo you are NOT in a git repo
fi
Run Code Online (Sandbox Code Playgroud)
但是,当我试图测试退出状态时,除了0我遇到问题时,这是其他任何事情.我尝试了以下,但没有一个工作:
if [[ "$(is_git_repository)" != "0" ]] ; ...总是评估为真(链接)if [[ "$(is_git_repository)" -ne "0" ]] ; ... 总是评估为假if [[ "$(is_git_repository)" != 0 ]] ; ... 总是评估为真if [[ "$(is_git_repository)" -ne 0 ]] ; ... 总是评估为假if …这似乎很基本,但我无法在文档中的任何地方找到它.我正在开发一个项目,我们通过shell脚本包装器运行一些测试:
./foo.sh a
./foo.sh b
./foo.sh c
Run Code Online (Sandbox Code Playgroud)
foo.sh不输出XUnit格式,因此我们需要一种不同的方式来向CircleCI发出故障信号.是exit 1(或任何非零退出代码)被识别为失败?什么条件导致CircleCI报告步骤失败?
我正在使用 GitKraken (不确定是否相关),当我尝试提交时,我收到以下错误消息:

当我按下“查看挂钩输出”按钮时,我得到以下信息:
预提交
husky > npm run -s 预提交(节点 v8.9.4)[?25l
npm > 运行 src/**/*.js 的任务
\ 纱线格式 git add (...这两行很多...)
\ 纱线格式 git add
“纱线格式”发现一些错误。请修复它们并再次尝试提交。纱线运行 v1.6.0 $ prettier-standard './src/**/*.js' C:\mypath\myfile.js info[访问 [0;1mhttps://yarnpkg.com/en/docs/cli/run有关此命令的文档。
prettier-eslint [错误] 由于 eslint 错误,eslint 修复失败 prettier-standard [错误] 格式化“C:\mypath\messages.js”时出现错误:TypeError:无法读取 null 属性“范围”
at SourceCode.getTokenBefore (c:\mypath\index.js:303:18)
at checkSpacingBefore (C:\mypath\template-curly-spacing.js:52:42)
at TemplateElement (C:\mypath\template-curly-spacing.js:117:17)
at listeners.(anonymous function).forEach.listener (C:\mypath\safe-emitter.js:47:58)
at Array.forEach (<anonymous>)
at Object.emit (C:\mypath\safe-emitter.js:47:38)
at NodeEventGenerator.applySelector (C:\mypath\node_modules\eslint\lib\utilode-event-generator.js:251:26)
at NodeEventGenerator.applySelectors (C:\mypath\node_modules\eslint\lib\util\node-event-generator.js:280:22)
at NodeEventGenerator.enterNode (C:\mypath\node-event-generator.js:294:14)
at CodePathAnalyzer.enterNode (C:\mypath\code-path-analyzer.js:608:23)
at Traverser.enter [as _enter] …Run Code Online (Sandbox Code Playgroud) exit-code ×10
bash ×4
shell ×4
python ×3
circleci ×1
codesign ×1
command ×1
git ×1
gitkraken ×1
if-statement ×1
inno-setup ×1
linux ×1
pre-commit ×1
scripting ×1
subprocess ×1
xcode ×1