bash命令分组,用于功能中的测试结果 [英] bash command grouping for test results in function

查看:85
本文介绍了bash命令分组,用于功能中的测试结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于使用这样的一行来测试变量是否具有非空值(或给出消息并保释):

I am accustomed to testing that a variable has a non-null value (or give a message and bail) using a line like this:

test $variable || (echo "Value of \$variable cannot be null."; exit 1)

我在脚本中使用函数还很陌生,但是我必须确保传递了非null值或将其从函数中释放出来.但是,或"情况的命令分组在函数内部的工作方式不同.我写了这个小测试脚本来演示:

I'm quite new to using functions in my scripts, but I've got a case where I need to be sure a non-null value is passed or bail out of the function. However the command grouping for the "or" case is not working the same way inside the function. I wrote this little test script to demonstrate:

 $ cat -n bubu.sh
 1  #!/bin/bash
 2
 3  read -p "give a value for variable \$foo: " -e -i "bar" foo
 4
 5  function firstfunc {
 6          test $1 || (echo "no value"; return 1)
 7          echo it looks like \"$1\" is  the first value of \$foo
 8          return 0
 9  }
10
11  function secondfunc {
12          test $1 || return 1
13          echo it looks like \"$1\" is the second value of \$foo
14          return 0
15  }
16  echo "first function:"
17  firstfunc $foo
18  echo returned $?
19
20  echo "second function:"
21  secondfunc $foo
22  echo returned $?

在变量具有值的情况下,输出如下所示:

And the output in the case where the variable has a value is like this:

$ ./bubu.sh
give a value for variable $foo: bar
first function:
it looks like "bar" is the first value of $foo
returned 0
second function:
it looks like "bar" is the second value of $foo
returned 0

在没有 值的情况下的输出

And the output in the case where there is not a value

$ ./bubu.sh
give a value for variable $foo:
first function:
no value
it looks like "" is the first value of $foo
returned 0
second function:
returned 1

在第一个函数中,当没有值时,我得到或"命令组的第一个命令,并且回显无值",但是传递了返回命令,并执行了该函数的其余部分返回.

In the first function when there is not a value I get the first command of the "or" command group, and "no value" echoes, but the return command is passed by and the rest of the function is executed, down to the return.

为什么在函数内部,用于命令分组的要素会有所不同,或者我还缺少什么?

Why are the parens for command grouping behaving differently inside the function, or what else am I missing?

推荐答案

正如其他人所提到的,parens创建一个子shell,exit或return命令指示bash从该子shell退出.您应该使用花括号.请注意,您可以使用bash参数替换以更简单的方式获得相同的效果.见下文.

As mentioned by others, the parens create a subshell and the exit or return command instructs bash to exit from the subshell. You should use curly braces. Note that you can use bash parameter substitution to get the same effect in a simpler way. See below.

您的行test $variable || (echo "Value of \$variable cannot be null."; exit 1)即使在函数上下文的外部"也不起作用.例如:

Your line test $variable || (echo "Value of \$variable cannot be null."; exit 1) should not work either even 'outside' of the context of a function. For instance:

$ cat t.sh 
#!/bin/bash

a="hello"
test $a || (echo "Value of \$variable cannot be null."; exit 1)
echo First test passed

a=
test $a || (echo "Value of \$variable cannot be null."; exit 1)
echo Second test passed

给予:

$ ./t.sh 
First test passed
Value of $variable cannot be null.
Second test passed
$ 

即使没有,第二个echo也会运行.使用花括号是一种方法.

Where we see that the second echo is run even though it shoulnd't have. Using curly braces is the way to go.

$ cat t.sh 
#!/bin/bash

a="hello"
test $a || { echo "Value of \$variable cannot be null."; exit 1 ; }
echo First test passed

a=
test $a || { echo "Value of \$variable cannot be null."; exit 1 ; }
echo Second test passed
$ ./t.sh 
First test passed
Value of $variable cannot be null.
$

请注意,您可以使用bash ${parameter:?err_msg}构造进行参数替换:

Note that you could use the bash ${parameter:?err_msg} construct for parameter substitution:

$ cat t.sh 
#!/bin/bash

a="hello"
: ${a:?'a is null or undefined! Exiting..'}
echo First test passed

a=
: ${a:?'a is null or undefined! Exiting..'}
echo Second test passed

$ ./t.sh 
First test passed
./t.sh: line 9: a: a is null or undefined! Exiting..
$

这篇关于bash命令分组,用于功能中的测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆