如何重新present多个条件中的s​​hell脚本? [英] How to represent multiple conditions in a shell script?

查看:146
本文介绍了如何重新present多个条件中的s​​hell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重新像这样的present多个条件:

I want to represent multiple conditions like this:

if [ ( $g -eq 1 -a "$c" = "123" ) -o ( $g -eq 2 -a "$c" = "456" ) ]   
then  
    echo abc;  
else  
    echo efg;   
fi  

但是当我执行该脚本,它显示了

but when I execute the script, it shows

syntax error at line 15: `[' unexpected, 

,其中线15是其中一张显示,如果....

where line 15 is the one showing if ....

什么是错的这个条件?我猜的东西是错误的()

What is wrong with this condition? I guess something is wrong with the ().

推荐答案

经典技术(逃跑元字符):

Classic technique (escape metacharacters):

if [ \( "$g" -eq 1 -a "$c" = "123" \) -o \( "$g" -eq 2 -a "$c" = "456" \) ]
then echo abc
else echo efg
fi

我包围的引用 $ G 在双引号;这是很好的做法,一般。严格地说,不需要括号因为precedence -a -o 使得它更正确没有他们。

I've enclosed the references to $g in double quotes; that's good practice, in general. Strictly, the parentheses aren't needed because the precedence of -a and -o makes it correct even without them.

请注意, -a -o 运营者的 测试 ,又名 [,主要用于向后兼容性(因为他们是测试在第七版UNIX,例如中的一部分),但是它们被明确标记为'过时' POSIX。巴什(见条件前pressions 的)似乎preempt经典和POSIX含义为 -a -o 与拿自己的其他运营商参数。

Note that the -a and -o operators are part of the POSIX specification for test, aka [, mainly for backwards compatibility (since they were a part of test in 7th Edition UNIX, for example), but they are explicitly marked as 'obsolescent' by POSIX. Bash (see conditional expressions) seems to preempt the classic and POSIX meanings for -a and -o with its own alternative operators that take arguments.

通过一些护理,可以用更现代的 [运营商,但要知道,在Bash和Korn Shell程序(例如)版本不必相同。

With some care, you can use the more modern [[ operator, but be aware that the versions in Bash and Korn Shell (for example) need not be identical.

for g in 1 2 3
do
    for c in 123 456 789
    do
        if [[ ( "$g" -eq 1 && "$c" = "123" ) || ( "$g" -eq 2 && "$c" = "456" ) ]]
        then echo "g = $g; c = $c; true"
        else echo "g = $g; c = $c; false"
        fi
    done
done

例来说,使用bash 3.2.57在Mac OS X:

Example run, using Bash 3.2.57 on Mac OS X:

g = 1; c = 123; true
g = 1; c = 456; false
g = 1; c = 789; false
g = 2; c = 123; false
g = 2; c = 456; true
g = 2; c = 789; false
g = 3; c = 123; false
g = 3; c = 456; false
g = 3; c = 789; false

您不需要引用变量 [为您提供办[,因为它不是以同样的方式单独命令 [

You don't need to quote the variables in [[ as you do with [ because it is not a separate command in the same way that [ is.

这难道不是一个经典的问题?

Isn't it a classic question?

我本来以为如此。然而,还有另一种替代方案,即:

I would have thought so. However, there is another alternative, namely:

if [ "$g" -eq 1 -a "$c" = "123" ] || [ "$g" -eq 2 -a "$c" = "456" ]
then echo abc
else echo efg
fi

事实上,如果你看过便携式外壳准则的autoconf 工具或相关的软件包,这个符号 - 使用 || &放大器;&安培; ' - 就是他们推荐什么。我想你甚至可以走这么远的:

Indeed, if you read the 'portable shell' guidelines for the autoconf tool or related packages, this notation — using '||' and '&&' — is what they recommend. I suppose you could even go so far as:

if [ "$g" -eq 1 ] && [ "$c" = "123" ]
then echo abc
elif [ "$g" -eq 2 ] && [ "$c" = "456" ]
then echo abc
else echo efg
fi

当行动是为呼应小巫见大巫,这是不坏。当要重复的动作块多行,重复太痛苦和早期版本之一是preferable - 或者你需要的操作包装成在不同调用的函数然后块。

这篇关于如何重新present多个条件中的s​​hell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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