1

I have the following shell script.

  if [ "$group" == "First*" ]]
  then
            OWNER_EMAIL=first-logs@ginger.com
  elif [ "$group"  == "Second*" ]]
  then
           OWNER_EMAIL=second-team@ginger.com
  fi

It does not throw any error , but does not execute the if statement properly if $group contains a First or a Second inside. Can any one tell me where I am going wrong.

user2890683
  • 403
  • 2
  • 6
  • 18
  • Have you tried `if [ "$group" = "First*" ]]` I think in shell programming one equals sign is enough. – Jens Aug 03 '14 at 07:05
  • firstly there is a syntax error you need to use either [ ] or [[ ]] secondly if the brackets are just typo then I think you want to use the wildcard * .. but when double quotes are used the wildcard looses it meaning – Ajay Aug 03 '14 at 07:30
  • possible duplicate of [How do you tell if a string contains another string in Unix shell scripting?](http://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-unix-shell-scripting) – user123444555621 Aug 03 '14 at 10:47

3 Answers3

1

Remove the quotes "" and it is done

if [ "$group" == First* ]
then
        OWNER_EMAIL=first-logs@ginger.com
 elif [ "$group"  == Second* ]
 then
       OWNER_EMAIL=second-team@ginger.com
 fi
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

You can also consider a case statement for efficiency:

case "$group" in
First*)
    OWNER_EMAIL=first-logs@ginger.com
    ;;
Second*)
    OWNER_EMAIL=second-team@ginger.com
    ;;
esac

You can add *) ... ;; as well for default action.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • why is this efficient? – user2890683 Aug 03 '14 at 07:37
  • 1
    @user2890683 `case` statements have less runtime overhead than other tests including `[[ ]]`. You also don't call a builtin twice for the test. And case statements are not even in the level of builtins. – konsolebox Aug 03 '14 at 07:39
0

When we use double quotes the wildcard character are not retained ... the check condition is looking for a exact "First*" and "Second*" match .

change your code in following way

if [[ "$group" == First* ]]
   then
        OWNER_EMAIL=first-logs@ginger.com
   elif [[ "$group"  == Second* ]]
   then
       OWNER_EMAIL=second-team@ginger.com
   fi
Ajay
  • 775
  • 5
  • 19