我想匹配字符串中包含“whitespaces”和“OR”条件的bash条件。我无法做,因为我是shell脚本的新手,请帮助,因为它转到其他匹配$ {myarrlin [1]}的循环。我得到“Centrify被禁用”,但我希望条件在这里成立。这是我的代码:
FILE_CENT="/etc/nsswitch.conf"
OS=`uname`
if [[ $OS = 'Linux' ]]; then
if [[ -e $FILE_CENT ]]; then
echo "nsswitch.conf found, Proceeding further..."
while read -r LINE
do
if [[ $LINE =~ ^passwd ]]; then
myarrlin=($LINE)
if [[ ${myarrlin[1]} =~ ^(centrify)|(centrifydc)[[:space:]]* || ${myarrlin[1]} =~ [[:space:]]+(centrify)|(centrifydc)[[:space:]]* ]]; then
echo "Centrify is enabled"
else
echo "Centrify is disabled"
fi
fi
done < $FILE_CENT
else
echo "nsswitch.conf does not exist in $OS, cannot fetch CENTRIFY information!"
fi
fi
nsswitch.conf&gt;&gt;&gt;
passwd: centrify files
or
passwd: centrifydc files
or
passwd: files centrify
or
passwd: files centrifydc
答案 0 :(得分:1)
你为什么这样做:myarrlin=($LINE)
?
如果您只是想知道该行是否包含centrify:
while read -r LINE
do
if [[ ${LINE} =~ ^passwd ]]; then
if [[ ${LINE} == *"centrify"* ]]; then
echo "Centrify is enabled"
else
echo "Centrify is disabled"
fi
fi
done < $FILE_CENT