【问题标题】:how to find and delete below line using shell script如何使用shell脚本查找和删除以下行
【发布时间】:2013-05-17 21:04:11
【问题描述】:

由于恶意攻击,下面一行已打印在我的所有 php 项目页面中。现在想想我如何使用 shell 脚本找到并删除这些行

function_exists('date_default_timezone') ?
  date_default_timezone_set('America/Los_Angeles') : 
    ($_REQUEST['c_id']));

我尝试使用以下脚本,但出现错误。我的意思是说我无法将上面的行与 sed 推荐匹配。请帮助我更正此脚本..

#!/bin/sh
search='^function_exists\(\'date_default_timezone\'\)\ \?\ date_default_timezone_set\(\'America/Los_Angeles\'\)\ \:\  \(\$_REQUEST\[\'c_id\'\]\)\)\;'

for file in `find /root/test1 -name "*.php"`; do grep "$search" $file &> /dev/null if [ $? -ne 0 ]; then echo "Search string not found in $file!" else sed -i '/$search/d' $file

【问题讨论】:

    标签: linux shell sed


    【解决方案1】:

    尝试 sed 使用 : 分隔符而不是 /,因为在您的模式中 America/La 与 /ir 冲突,因此添加反斜杠使其 America/la

    【讨论】:

      【解决方案2】:

      您没有正确转义正则表达式。请尝试以下操作:

      while IFS= read -r -d '' file; do
          if grep -qF "function_exists('date_default_timezone') ? date_default_timezone_set('America/Los_Angeles') : (\$_REQUEST['c_id']));" "$file"
          then
              sed -i "s|function_exists('date_default_timezone') ? date_default_timezone_set('America/Los_Angeles') : (\$_REQUEST\['c_id'\]));|FOO|g" "$file"
          fi
      done < <(find /root/test1 -type f -name "*.php" -print0)
      

      【讨论】:

        【解决方案3】:

        这可能对你有用(GNU sed)

        pattern1='function_exists('\''date_default_timezone'\'''
        pattern2='.*date_default_timezone_set('\''America\/Los_Angeles'\'') :'
        pattern3='.*($_REQUEST\['\''c_id'\''\]));'
        sed '/^'"$pattern1"'/{N;N;/^'"$pattern1$pattern2$pattern3"'/d}' file
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-23
          • 2015-06-17
          • 2018-12-22
          • 2012-11-14
          • 2016-12-27
          相关资源
          最近更新 更多