title: "Too many arguments" issue in if statement in Bash
body: <p>I'm working on a script in order to automate a license re-activation. Instead of doing this manually every 40 days I want the script to do it for me. </p>
  
  <p>I have added licenses to licenses.txt and the script builds a variable from the first line of the licenses.txt which is uses with the command SOAPLicenseClient. If the command is successful it will then remove that line from the file. Once the licenses.txt is running low on licenses it will send a log message to log facility local0. </p>
  
  <p>When the command is successful it will print:</p>
  
  <blockquote>
    <p>New license installed</p>
  </blockquote>
  
  <p>and when it is trying to install a license that has already been installed previously it will print:</p>
  
  <blockquote>
    <p>License server has returned an exception. Fault code: 51092 Fault text: Error 51092, This license has already been activated on a different unit. Please contact technical support for assistance</p>
  </blockquote>
  
  <p>If it receives this error it will then remove the first line of the licenses.txt file and reuse the SOAPLicenseClient command in order to try a new license. </p>
  
  <p>At the end I have a catch all that will simply print the output from $results into the log facility 0.</p>
  
  <p>Here is the entire script:</p>
  
  <pre><code>#VARIABLES
  BaseReg=$(head -n1 licenses.txt)
  currentlicensecount=$(wc -l &lt; licenses.txt)
  licensewarninglimit=2
  nolicenses=0
  
  if ! [ -f "/config/licenses.txt" ]
      then 
      /bin/logger -p local0.error "License reactivation failed - Licensefile is MISSING - Please add a Licensefile to /config/licenses.txt"
  fi
  
  if [ $currentlicensecount -eq $nolicenses ]
      then
          /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $currentlicensecount - Please add more licenses"
      else 
      result=$(SOAPLicenseClient --basekey "$BaseReg")
          if [ $result -eq "New license installed" ]
              then
              #Removing the recently used BaseRegistration key from the licenes.txt       
              tail -n +2 licenses.txt &gt; licenses.tmp &amp;&amp; mv licenses.tmp licenses.txt
              newlicensecount=$(wc -l &lt; licenses.txt)
              /bin/logger -p local0.notice "License reactivation succeded - "$result" - Remaining Licenses: $newlicensecount"
                  if [ $newlicensecount -lt $licensewarninglimit ]
                      then
                      /bin/logger -p local0.warning "Current Licensecount is below warning threshold - Remaining Licenses: $newlicensecount - Please add more licenses"
                  elif [ $newlicensecount -eq $nolicenses ]
                      then
                      /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $newlicensecount - Please add more licenses"
                  fi
          elif [ $result -eq "License server has returned an exception. Fault code: 51092 Fault text: Error 51092, This license has already been activated on a different unit. Please contact technical support for assistance" ]
              then
              #Removing the recently used BaseRegistration key from the licenes.txt       
              tail -n +2 licenses.txt &gt; licenses.tmp &amp;&amp; mv licenses.tmp licenses.txt
              NewBaseReg=$(head -n1 licenses.txt)
              newresult=$(SOAPLicenseClient --basekey "$NewBaseReg")
              if [ $newresult -eq "New license installed" ]
                  then
                  #Removing the recently used BaseRegistration key from the licenes.txt       
                      tail -n +2 licenses.txt &gt; licenses.tmp &amp;&amp; mv licenses.tmp licenses.txt
                      newlicensecount=$(wc -l &lt; licenses.txt)
                      /bin/logger -p local0.notice "License reactivation succeded - "$result" - Remaining Licenses: $newlicensecount"
                          if [ $newlicensecount -lt $licensewarninglimit ]
                              then
                              /bin/logger -p local0.warning "Current Licensecount is below warning threshold - Remaining Licenses: $newlicensecount - Please add more licenses"
                          elif [ $newlicensecount -eq $nolicenses ]
                              then
                              /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $newlicensecount - Please add more licenses"
                          fi
          else
          /bin/logger -p local0.error "License reactivation failed - "$result" - Remaining Licenses: $currentlicensecount"
                  if [ $currentlicensecount -lt $licensewarninglimit ]
                      then
                      /bin/logger -p local0.warning "Current Licensecount is below warning threshold - Remaining Licenses: $currentlicensecount - Please add more licenses"
                  elif [ $currentlicensecount -eq $nolicenses ]
                      then
                      /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $currentlicensecount - Please add more licenses"
                  fi
  
          fi
      fi
  fi
  </code></pre>
  
  <p>When running the script I receive the following errors:</p>
  
  <pre><code># sh ./license_reactivation.sh
  ./license_reactivation.sh: line 20: [: too many arguments
  ./license_reactivation.sh: line 33: [: too many arguments
  </code></pre>
  
  <p>The lines it is complaining about is:</p>
  
  <pre><code>if [ $result -eq "New license installed" ]
  
  elif [ $result -eq "License server has returned an exception. Fault code: 51092 Fault text: Error 51092, This license has already been activated on a different unit. Please contact technical support for assistance" ]
  </code></pre>
  
  <p>From what I can tell based on searching here is that the variable $result contains too many arguments due to the fact that it contains several different words with spaces. </p>
  
  <p>Using a more simple script I echo out the $results variable:</p>
  
  <p>Script:</p>
  
  <pre><code>#VARIABLES
  BaseReg=$(head -n1 licenses.txt)
  currentlicensecount=$(wc -l &lt; licenses.txt)
  licensewarninglimit=2
  nolicenses=0
  
  if ! [ -f "/config/licenses.txt" ]
      then 
      /bin/logger -p local0.error "License reactivation failed - Licensefile is MISSING - Please add a Licensefile to /config/licenses.txt"
  fi
  
  if [ $currentlicensecount -eq $nolicenses ]
      then
      /bin/logger -p local0.error "No licenses available! - Remaining Licenses: $currentlicensecount - Please add more licenses"
      else 
          result=$(SOAPLicenseClient --basekey "$BaseReg")
          echo $result
  fi
  </code></pre>
  
  <p>Here is the results:</p>
  
  <pre><code># sh ./license_reactivation.sh
  License server has returned an exception. Fault code: 51092 Fault text: Error 51092, This license has already been activated on a different unit. Please contact technical support for assistance
  </code></pre>
  
  <p>Is it possible for me to filter out strings from $results and create new variables? I tried sed, awk and grep but I'm doing something wrong. </p>
  
  <p>What is the most effective way of doing this? The code may not be pretty and if you have improvements feel free to speak up.</p>