Bash script 'sed: first RE may not be empty' error

Posted by Arica Deslauriers on Wednesday, May 22, 2024

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.

When I run the script it returns the following error:

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 

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.

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?

#!/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 "<?php echo Mage::helper('adminhtml')->__('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\" ?><?php echo \""$OLD_NUM NEW_NUM_FULL="password\" ?><?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 "<?php echo \"(password\" ?><?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->setNewPassword(password"$NEW_NUM_GREPP")" sed -ie "s/$SET_NEW_GREP/$FULL_NEW_PASS/g" $i done 

Thanks in advance for any help with this.

UPDATE -- ANSWER

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:

#!/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 "<?php echo Mage::helper('adminhtml')->__('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\" ?><?php echo \""$OLD_NUM NEW_NUM_FULL="password\" ?><?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 "<?php echo \"(password\" ?><?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->setNewPassword(password"$NEW_NUM_GREPP")" sed -ie "s/$SET_NEW_GREP/$FULL_NEW_PASS/g" $FIND_IN_DIRECTORIES done 
1

1 Answer

without debugging your whole setup, note that you can use an alternate character to delimit sed reg-ex/match values, i.e.

 sed -i "s\@$OLD_NUM_FULL@$NEW_NUM_FULL@g" $i 

and

 sed -i "s\@$SET_NEW_GREP@$FULL_NEW_PASS@g" $i 

You don't need the -e, so I have removed it.

Some seds require the leading '\' before the @, so I include it. It is possible that some will be confused by it, so if this doesn't work, try removing the leading '\'

you should also turn on shell debugging, to see exactly which sed (and what values) are causing the problem. Add a line with set -vx near the top of your script to turn on debugging.

I hope this helps.

1

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJocGxsY2uGc3vBmqqhZaOYv6q802aqnpxdm7azv9NmqZ5lnZbGbrrOrWSbnV2aurHA2Gacq6qfpw%3D%3D