本文翻译自:How to represent multiple conditions in a shell if statement?

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,

where line 15 is the one showing if .... 第15行显示的是....

What is wrong with this condition? 这种情况怎么了? I guess something is wrong with the () . 我猜想()


#1楼

参考:https://stackoom.com/question/g3qx/如何在if语句中表示多个条件


#2楼

Be careful if you have spaces in your string variables and you check for existence. 如果字符串变量中有空格并检查是否存在,请格外小心。 Be sure to quote them properly. 确保正确引用它们。

if [ ! "${somepath}" ] || [ ! "${otherstring}" ] || [ ! "${barstring}" ] ; then

#3楼

Using /bin/bash the following will work: 使用/bin/bash可以正常工作:

if [ "$option" = "Y" ] || [ "$option" = "y" ]; thenecho "Entered $option"
fi

#4楼

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

I've enclosed the references to $g in double quotes; 我已将对$g的引用括在双引号中; 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的优先级使得即使没有它们也正确。

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. 请注意, -a-o运营商的POSIX规范的一部分test ,又名[ ,主要用于向后兼容性(因为他们的部分test在第7版UNIX等),但它们被明确标记为“过时由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(请参阅条件表达式 )似乎以其自己的带参数的替代运算符抢占了-a-o的经典含义和POSIX含义。


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. 一定要小心,您可以使用更现代的[[运算符,但是请注意,例如Bash和Korn Shell中的版本不必相同。

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

Example run, using Bash 3.2.57 on Mac OS X: 在Mac OS X上使用Bash 3.2.57运行示例:

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

Indeed, if you read the 'portable shell' guidelines for the autoconf tool or related packages, this notation — using ' || 确实,如果您阅读了autoconf工具或相关软件包的“便携式外壳”指南,则使用- ||表示。 ' 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

Where the actions are as trivial as echoing, this isn't bad. 在动作像回声一样琐碎的情况下,这还不错。 When the action block to be repeated is multiple lines, the repetition is too painful and one of the earlier versions is preferable — or you need to wrap the actions into a function that is invoked in the different then blocks. 当要重复执行的动作块为多行时,重复会很痛苦,并且较早的版本之一是可取的-否则您需要将动作包装到在不同的then块中调用的函数中。


#5楼

在Bash中:

if [[ ( $g == 1 && $c == 123 ) || ( $g == 2 && $c == 456 ) ]]

#6楼

$ g=3
$ c=133
$ ([ "$g$c" = "1123" ] || [ "$g$c" = "2456" ]) && echo "abc" || echo "efg"
efg
$ g=1
$ c=123
$ ([ "$g$c" = "1123" ] || [ "$g$c" = "2456" ]) && echo "abc" || echo "efg"
abc

如何在if语句中表示多个条件?相关推荐

  1. 转在同一个sql语句中如何写不同条件的count数量

    今天在做Portal中的Dashboard展现的时候,需要对多个统计字段做展现,根据我现在的掌握水平,我只能在sql调用构建器中实现一种sql语 句返回的resultSet做展现.没有办法,只能从数据 ...

  2. oracle大于条件,oracle中sql语句中的in的条件数量大于1000有问题

    oracle中sql语句中的in的条件数量大于1000有问题 oracle中sql语句中select * from t_Test t where  t.Id in(1,2,3......)/*数量不能 ...

  3. oracle select 重命名,如何在SELECT语句中重命名和引用COUNT(*)?

    我正在尝试在SELECT语句中使用COUNT(*).但是,我需要重新命名它,并能够为WHERE子句引用它. 我尝试过使用AS,我尝试省略AS,因为根据oracle页面似乎没有必要:https://do ...

  4. linux脚本多个条件比较大小,如何在shell if语句中表示多个条件?

    经典技术(转义元字符): if [ \( "$g" -eq 1 -a "$c" = "123" \) -o \( "$g" ...

  5. lisp语言cond和if套用_在'if'语句中设置多行条件的样式?

    Harley Holco.. 679 您不需要在第二个条件行上使用4个空格.也许用: if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val ...

  6. 在c51语言的循环语句中 用作循环结束,在C51语言的循环语句中,用作循环结束条件判断的表达式为()...

    的任营领域都个战企业为结何一合的略经是以,语言用作战略经营须综合考领域虑这两个分析方面时必,. 循的表达式的一参与资产战略个以共同个独企业企业建立就是或两两个立的联盟上的. 环语对方的盟就盟各一家业( ...

  7. 【力荐】Select查询语句中LIKE关键词的优化方法分析

    今天接到一个优化需求,跑个程序要12+个小时,周期是每天一次,所以时效性极差,不能响应快速的实际业务需求,下面我们看一段LIKE的优化方法. SELECT     bukrs werks lgort ...

  8. 在ABAP的SQL语句中写Oracle Hints

    在ABAP的SQL语句中写Oracle Hints ①用过的两个写法: 1.指定使用全表扫描:%_HINTS ORACLE 'FULL(table_name)' 2.指定索引:%_HINTS ORAC ...

  9. sql语句中case_SQL中的CASE语句

    sql语句中case The case statement in SQL returns a value on a specified condition. We can use a Case sta ...

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(20)-权限管理系统-根据权限获取菜单
  2. 比英伟达便宜4000元、功耗更低、游戏性能相同,AMD发布RX 6900 XT旗舰显卡
  3. Oracle数据库导入导出命令
  4. AI基础:机器学习简易入门
  5. 《网络编程》ioctl 操作
  6. 前端学习(1933)vue之电商管理系统电商系统之优化树形控件
  7. PHP两个日期之间的所有日期
  8. 不懂得使用工具的测试不是好测试
  9. 面向对象(Python):学习笔记之继承
  10. 通过linux访问IPMI端口,Linux通过命令行设置IPMI的解决方法(图)
  11. Tomcat7升级到Tomcat9
  12. python科赫雪花递归理解_科赫雪花的python3实现(递归,非递归)
  13. 一阶电路误差分析_自动控制(3)时域分析
  14. 289714-02-9,Biotin-PEG3-alcohol,Biotin-PEG3-OH含有一个生物素基团和一个与多种官能团反应的末端伯羟基
  15. 获取手机或电脑GPS位置信息(定位平台)
  16. 键盘上一些常用按键的ASCII码值
  17. 计算机图形学基础1——MVP变换
  18. 智能摄像头为什么受到这么多人青睐?米家、智汀带你了解一下
  19. 关于自制utau软件,widegt,动态更改控件长度等等经验
  20. html5 app如何连接打印机,uni-app开发经验分享十五: uni-app 蓝牙打印功能

热门文章

  1. 匈牙利算法的MATLAB实现
  2. Chrome浏览器如何设置代理?如何快速切换代理?
  3. 记录总结-如何逮捕实验室的老鼠
  4. 如何批量提取图片的名称?亲测好用
  5. 卡方检验 两分类实现
  6. 深入学习高频脉冲变压器GDT的设计
  7. Angular2 中用管道技术吧汉字转换为拼音
  8. 日期时间选择器---hh代表是12小时制,HH表示24小时制
  9. CAXA 电子图版 二次开发 用vs2005 2008 2010 编译
  10. 除了平台和功能外,企业年会直播方案还要考虑哪些内容?