是否有存储和计算返回值的bash脚本优雅的方式? [英] Is there an elegant way to store and evaluate return values in bash scripts?
问题描述
我有一个相当复杂的一系列bash中的最终返回一个有意义的退出code命令。各个地方在后面的脚本需要分支有条件的命令集成功与否。
I have a rather complex series of commands in bash that ends up returning a meaningful exit code. Various places later in the script need to branch conditionally on whether the command set succeed or not.
目前我储存的退出code和数字测试它,像这样:
Currently I am storing the exit code and testing it numerically, something like this:
long_running_command | grep -q trigger_word
status=$?
if [ $status -eq 0 ]; then
: stuff
else
: more code
if [ $status -eq 0 ]; then
: stuff
else
出于某种原因,感觉这应该是简单。我们有一个存储简单的退出code,现在我们正在重复输入了数值实验操作在其上运行。例如,我能骗来代替返回code这是简单的测试字符串输出:
For some reason it feels like this should be simpler. We have a simple exit code stored and now we are repeatedly typing out numerical test operations to run on it. For example I can cheat use the string output instead of the return code which is simpler to test for:
status=$(long_running_command | grep trigger_word)
if [ $status ]; then
: stuff
else
: more code
if [ $status ]; then
: stuff
else
在表面上,这看起来更直截了当,但我意识到这是脏的。
On the surface this looks more straight forward, but I realize it's dirty.
如果其他的逻辑是没有那么复杂,我只是跑这个曾经,我知道我可以代替测试运营商的嵌入,但是当你需要重复使用在其他位置的结果<强这是不理想>不必重新运行测试
If the other logic wasn't so complex and I was only running this once, I realize I could embed it in place of the test operator, but this is not ideal when you need to reuse the results in other locations without re-running the test:
if long_running_command | grep -q trigger_word; then
: stuff
else
我到目前为止发现的唯一的事情就是为命令替换的一部分分配code:
The only thing I've found so far is assigning the code as part of command substitution:
status=$(long_running_command | grep -q trigger_word; echo $?)
if [ $status -eq 0 ]; then
: stuff
else
即使这不是技术上的一次性分配(虽然有些人可能认为可读性更好),但必要的数值试验语法似乎仍然很麻烦给我。也许我只是强迫症。
Even this is not technically a one shot assignment (although some may argue the readability is better) but the necessary numerical test syntax still seems cumbersome to me. Maybe I'm just being OCD.
我失去了一个更优雅的方式来分配出口code到一个变量再后来就IT部门?
推荐答案
简单的解决方案:
output=$(complex_command)
status=$?
if (( status == 0 )); then
: stuff with "$output"
fi
: more code
if (( status == 0 )); then
: stuff with "$output"
fi
或者更eleganter十岁上下
Or more eleganter-ish
do_complex_command () {
# side effects: global variables
# store the output in $g_output and the status in $g_status
g_output=$(
command -args | commands | grep -q trigger_word
)
g_status=$?
}
complex_command_succeeded () {
test $g_status -eq 0
}
complex_command_output () {
echo "$g_output"
}
do_complex_command
if complex_command_succeeded; then
: stuff with "$(complex_command_output)"
fi
: more code
if complex_command_succeeded; then
: stuff with "$(complex_command_output)"
fi
或者
do_complex_command () {
# side effects: global variables
# store the output in $g_output and the status in $g_status
g_output=$(
command -args | commands
)
g_status=$?
}
complex_command_output () {
echo "$g_output"
}
complex_command_contains_keyword () {
complex_command_output | grep -q "$1"
}
if complex_command_contains_keyword "trigger_word"; then
: stuff with "$(complex_command_output)"
fi
这篇关于是否有存储和计算返回值的bash脚本优雅的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!