我想在这样的if语句中测试bash函数的返回值:
if [[ func arg ]] ; then …
但我收到如下错误消息: conditional binary operator expected.
什么是正确的方法?
是吗:
if [[ $(func arg) ]] ; then ...
我想在这样的if语句中测试bash函数的返回值:
if [[ func arg ]] ; then …
但我收到如下错误消息: conditional binary operator expected.
什么是正确的方法?
是吗:
if [[ $(func arg) ]] ; then ...
Answers:
如果是退出代码而不是结果,则可以使用
if func arg; then ...
如果无法使函数返回正确的退出代码(带有return N
),并且必须使用字符串结果,请使用@Alex Gitelman答案。
$ help if
:
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
根据条件执行命令。
该
if COMMANDS
列表已执行。如果其退出状态为零,则then COMMANDS
执行列表。否则,elif COMMANDS
将依次执行每个列表,如果其退出状态为零,则将then COMMANDS
执行相应的 列表,并且if命令将完成。否则,将else COMMANDS
执行列表(如果存在)。整个构造的退出状态是最后执行的命令的退出状态,如果没有条件测试为真,则为零。退出状态:返回上一次执行的命令的状态。
if !func arg;
不起作用。
if func ; then ; else ...; fi
我希望有更好的东西:)不过谢谢。
if ! func arg; then echo "func returns non zero"; fi
注意求反运算符和函数调用之间的空间
这对我很有用,因此我将添加以下详细信息。
如果需要测试两个条件,一个条件是功能/命令的退出状态,另一个条件是变量的值,请使用以下条件:
if func arg && [[ $foo -eq 1 ]]; then echo TRUE; else echo FALSE; fi
如果函数返回多个单词,则似乎会产生此错误。
例如,1 2
。
引用一下即可:
"$(func arg)"
样品:
$ if [[ 1 2 ]] ; then echo 1 ; fi
-bash: conditional binary operator expected
-bash: syntax error near `2'
$ if [[ "1 2" ]] ; then echo 1 ; fi
1
如果比较0与非0,请使用
if [[ "$(func arg)" != "0" ]]
与此相关的是,如果函数返回各种退出代码而不是true / false,则:
func args; ec=$? # call function and grab the exit code
# it is better to have them on the same line so that a future addition of a command
# before the case statement wouldn't break the logic
case $ec in
value1) # commands
;;
value2) # commands
;;
*) # commands
;;
esac
意识到这是一个老帖子...这是我对此事的建议:
select
在这里提供了很多帮助。
PS3="What's your choice? (^D to stop choosing): "
select mainmenuinput in updatesystem installsamba installvsftpd installwebmin configuresambaforactivedirectory quitprogram; do
case "$mainmenuinput" in
"updatesystem")
echo "Update System..."
;;
"installsamba")
echo "Installing Samba..."
;;
#echo And so forth...
esac
done
echo Done
如需有关的帮助select
,请咨询man bash
并搜索“选择”。不提供任何输入将重复菜单。
select name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the
positional parameters are printed (see PARAMETERS below). The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of
the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read
causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed. The exit status of select is the
exit status of the last command executed in list, or zero if no commands were executed.
样本输出:
[rinzler ~] $ ./test.sh
1) updatesystem 4) installwebmin
2) installsamba 5) configuresambaforactivedirectory
3) installvsftpd 6) quitprogram
What's your choice? (^D to stop choosing): 1
Update System...
What's your choice? (^D to stop choosing): 2
Installing Samba...
What's your choice? (^D to stop choosing):
1) updatesystem 4) installwebmin
2) installsamba 5) configuresambaforactivedirectory
3) installvsftpd 6) quitprogram
What's your choice? (^D to stop choosing):
Done