在Bash Scripting中使用“OR”操作时没有得到满足

时间:2012-12-20 11:34:44

标签: bash string-comparison

我的操作系统:Fedora第13版(戈达德)

'test'下有5个目录

[root@localhost lab]# ls test
dir1  dir2  dir3  dir4  dir5

SCRIPT1

#!/bin/bash
#below line for listing all directories under '/home/arun/lab/test'
ls -Al --time-style=long-iso /home/arun/lab/test | grep '^d' | awk '{print $8}' | while read line
do
#Need to skip few directoirs
if [ "$line" == "dir1" -o "$line" == "dir2" ]; then
continue
fi
#will take some actions here..as of now just echo only
echo $line
done

Script2 [仅修改已更改==至!=]

#!/bin/bash
#below line for listing all directories under '/home/arun/lab/test'
ls -Al --time-style=long-iso /home/arun/lab/test | grep '^d' | awk '{print $8}' | while read line
do
#Need to skip few directoirs
if [ "$line" != "dir1" -o "$line" != "dir2" ]; then
continue
fi
#will take some actions here..as of now just echo only
echo $line
done

脚本1给出预期的o / p

[root@localhost lab]# ./test.sh 
dir3
dir4
dir5

问题是: 对于脚本2,我期待低于o / p。但是跑步时没有o / p [空白] ..请帮助我。

[root@localhost lab]# ./test.sh 
dir1
dir2

1 个答案:

答案 0 :(得分:3)

对于第二个脚本,您需要-a(“和”)而不是-o(“或”)。通过思考逻辑:每个可能的字符串不等于两者中的至少一个,但是当它不等于 时,你想要跳过它,也就是说当它不等于“ dir1“它也不等于”dir2“。