title: Bash script 'sed: first RE may not be empty' error
body: <p>I have written the following bash script, it is not finished yet so it is still a little messy. The script looks for directories at the same level as the script, it then searches for a particular file within the directory which it makes some changes to. </p>
  
  <p>When I run the script it returns the following error:</p>
  
  <pre><code>sed: first RE may not be empty
  sed: first RE may not be empty
  sed: first RE may not be empty
  sed: first RE may not be empty
  sed: first RE may not be empty
  sed: first RE may not be empty
  sed: first RE may not be empty
  </code></pre>
  
  <p>My research tells me that it may be something to do with the '/'s in the directory name strings but I have not been able to solve the issue.</p>
  
  <p>Despite the error messages the script seems to be working fine and is making the changes to the files correctly. Can anyone help explain why I am getting the error message above? </p>
  
  <pre><code>#!/bin/bash
  
  FIND_DIRECTORIES=$(find . -type d -maxdepth 1 -mindepth 1)
  FIND_IN_DIRECTORIES=$(find $FIND_DIRECTORIES"/app/design/adminhtml" -name "login.phtml")
  
  for i in $FIND_IN_DIRECTORIES
    do
      # Generate Random Number
      RANDOM=$[ ( $RANDOM % 1000 )  + 1 ]
  
      # Find the line where password is printed out on the page
      # Grep for the whole line, then remove all but the numbers
      # This will leave the old password number
      OLD_NUM_HOLDER=$(cat $i | grep "&lt;?php echo Mage::helper('adminhtml')-&gt;__('Password: ')" )
      OLD_NUM="${OLD_NUM_HOLDER//[!0-9]}"
  
      # Add old and new number to the end of text string
      # Beginning text string is used so that sed can find
      # Replace old number with new number
      OLD_NUM_FULL="password\" ?&gt;&lt;?php echo \""$OLD_NUM
      NEW_NUM_FULL="password\" ?&gt;&lt;?php echo \""$RANDOM
      sed -ie "s/$OLD_NUM_FULL/$NEW_NUM_FULL/g" $i
  
      # GREP for the setNewPassword function line
      # GREP for new password that has just been set above
      SET_NEW_GREP=$(cat $i | grep "setNewPassword(" )
      NEW_NUM_GREP=$(cat $i | grep "&lt;?php echo \"(password\" ?&gt;&lt;?php echo" )
      NEW_NUM_GREPP="${NEW_NUM_GREP//[!0-9]}"
  
      # Add new password to string for sed
      # Find and replace old password for setNewPassword function
      FULL_NEW_PASS="\$user-&gt;setNewPassword(password"$NEW_NUM_GREPP")"
      sed -ie "s/$SET_NEW_GREP/$FULL_NEW_PASS/g" $i
    done
  </code></pre>
  
  <p>Thanks in advance for any help with this.</p>
  
  <h2>UPDATE -- ANSWER</h2>
  
  <p>The issue here was that the for loop was not working as expected. I thought that it was doing /first/directory"/app/design/adminhtml" looping through and then doing /second/directory"/app/design/adminhtml" and then looping through. It was actually doing /first/directory looping through and then doing /second/directory"/app/design/adminhtml" and then looping through. So it was actually attaching the full directory path to the last item in the iteration. I have fixed the issue in the script below:</p>
  
  <pre><code>#!/bin/bash
  
  for i in $(find . -type d -maxdepth 1 -mindepth 1); do
    FIND_IN_DIRECTORIES=$i"/app/design/adminhtml/default"
    FIND_IN_DIRECTORIES=$(find $FIND_IN_DIRECTORIES -name "login.phtml")
  
    # Generate Random Number
    RANDOM=$[ ( $RANDOM % 1000 ) + 1 ]
  
    # Find the line where password is printed out on the page
    # Grep for the whole line, then remove all but the numbers
    # This will leave the old password number
    OLD_NUM_HOLDER=$(cat $FIND_IN_DIRECTORIES | grep "&lt;?php echo Mage::helper('adminhtml')-&gt;__('Password: ')" )
    OLD_NUM="${OLD_NUM_HOLDER//[!0-9]}"
  
    # Add old and new number to the end of text string
    # Beginning text string is used so that sed can find
    # Replace old number with new number
    OLD_NUM_FULL="password\" ?&gt;&lt;?php echo \""$OLD_NUM
    NEW_NUM_FULL="password\" ?&gt;&lt;?php echo \""$RANDOM
    sed -ie "s/$OLD_NUM_FULL/$NEW_NUM_FULL/g" $FIND_IN_DIRECTORIES
  
    # GREP for the setNewPassword function line
    # GREP for new password that has just been set above
    SET_NEW_GREP=$(cat $FIND_IN_DIRECTORIES | grep "setNewPassword(" )
    NEW_NUM_GREP=$(cat $FIND_IN_DIRECTORIES | grep "&lt;?php echo \"(password\" ?&gt;&lt;?php echo" )
    NEW_NUM_GREPP="${NEW_NUM_GREP//[!0-9]}"
  
    # Add new password to string for sed
    # Find and replace old password for setNewPassword function
    FULL_NEW_PASS="\$user-&gt;setNewPassword(password"$NEW_NUM_GREPP")"
    sed -ie "s/$SET_NEW_GREP/$FULL_NEW_PASS/g" $FIND_IN_DIRECTORIES
  done
  </code></pre>