简体   繁体   中英

how to find and delete below line using shell script

Below line has printed in my all php project pages because of malicious attacks.Now think is how i can find and delete this lines using shell script

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

I tried with below script but i getting error.I mean to say I not able to match above line with sed commend.Please help me to correct this script..

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

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

You're not escaping the regex correctly. Try the following:

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)

This might work for you (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM