Skip to content
Advertisement

How to match “whitespace” and “OR” condition with bash script?

I want to match a condition in bash containing “whitespaces” and “OR” condition within strings. I am unable to do as i am new to shell scripting, please help as it going to else loop where it is matching ${myarrlin[1]} . I am getting “Centrify is disabled” but i want the condition to be true here. Here is my code :

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 >>>

passwd:     centrify files
  or
passwd:     centrifydc files
  or
passwd:     files centrify
  or
passwd:     files centrifydc

Advertisement

Answer

Why are you doing this: myarrlin=($LINE) ?

If you just want to know if the line contains 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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement