Bash脚本'sed:第一个RE可能不为空'错误

时间:2012-02-18 18:02:16

标签: bash sed

我已经编写了以下bash脚本,它还没有完成,所以它仍然有点凌乱。该脚本查找与脚本处于同一级别的目录,然后在目录中搜索对其进行某些更改的特定文件。

当我运行脚本时,它返回以下错误:

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

我的研究告诉我,这可能与目录名字符串中的'/'有关,但我无法解决问题。

尽管出现错误消息,但脚本似乎工作正常并且正确地对文件进行了更改。任何人都可以帮助解释为什么我收到上面的错误消息?

#!/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

提前感谢您提供任何帮助。

更新 - 答案

这里的问题是for循环没有按预期工作。我认为它正在/ first /目录“/ app / design / adminhtml”循环然后执行/ second /目录“/ app / design / adminhtml”然后循环。它实际上正在执行/ first /目录循环,然后执行/ second /目录“/ app / design / adminhtml”然后循环。所以它实际上是将完整的目录路径附加到迭代中的最后一项。我在下面的脚本中解决了这个问题:

#!/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 个答案:

答案 0 :(得分:1)

没有调试整个设置,请注意您可以使用替代字符来分隔sed reg-ex / match值,即

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

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

你不需要-e,所以我删除了它。

有些seds需要@之前的前导'\',所以我加入它。有些人可能会对它感到困惑,所以如果这不起作用,请尝试删除前导'\'

您还应该启用shell调试,以确切了解导致问题的sed(和值)。在脚本顶部附近添加一行set -vx以启用调试功能。

我希望这会有所帮助。