bash shell的if语句如何使用多个判断

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
thlgood
帖子: 37
注册时间: 2010-12-28 9:13

bash shell的if语句如何使用多个判断

#1

帖子 thlgood » 2012-02-17 8:14

现在我的目的是这样的,当$imageName同时不在waiting和history这两个文件的时候逻辑为真。
我的代码是这样写的

代码: 全选

if grep -qv $imageName waiting && grep -qv $imageName history; then
        ……
else
        ……
fi
:em05 为何这样不行? 正确的写法该怎么写?
头像
thlgood
帖子: 37
注册时间: 2010-12-28 9:13

Re: bash shell的if语句如何使用多个判断

#2

帖子 thlgood » 2012-02-17 13:10

我已经找到解决办法了。

代码: 全选

if ! ( grep -q $lines scanHistory || grep -q $lines waiting ) ; then
        ....
fi
头像
Think1st
帖子: 45
注册时间: 2012-02-07 23:08

Re: bash shell的if语句如何使用多个判断

#3

帖子 Think1st » 2012-02-20 15:36

我使用两个都可以。我是bash 4.1版本。
这个也可以。

代码: 全选

  
  7 if [[ `grep -qv $imageName file1 ` && `grep -qv $imageName file2` ]] ; then
  8         echo haha;
  9 else
 10         echo hoho;
 11 fi                                                                         

在这里,问题比答案更抢手。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: bash shell的if语句如何使用多个判断

#4

帖子 eexpress » 2012-02-20 15:48

[] && xxx || yyy
一样,还简洁。
● 鸣学
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: bash shell的if语句如何使用多个判断

#5

帖子 aerofox » 2012-02-22 19:00

再来一个:

代码: 全选

if cat waiting history | grep -qv $imageName; then
        ……
else
        ……
fi
回复