undefinedfix
Sign in

How to use Linux command to batch add PHP code under a line of code

Toktam edited in Wed, 25 May 2022

There are about 10 of the same stations / data / www/ aa.com/test.php/data/www/bb.com/test.php/data/www/cc.com/test.php Want to use Linux command to batch modify / data / www/ aa.com/test.php Add the following code under a specified line of the file $m = $this - > model ('article ')

        $M->AddNewsHits(array("id"=>(int)$_GET['id']));
        $news_info=$M->GetNewsBaseOnce(array("id"=>(int)$_GET['id']),array("field"=>"hits"));
        echo "document.write('".$news_info["hits"]."')";

There is a better way to write with Linux shell command

2 Replies
suxbr
commented on Wed, 25 May 2022

Shell script did not think of a way to provide a PHP processing ideas for you. You put / data / www/ bb.com/test . PHP file code, fgets ($FP) out, and then processed into an array $code [$num] = [...]. Then in the row you want to join, use array_ splice($code, $num, 0, $newcode)。 After processing, it is processed into a string and re written into / data / www/ bb.com/test . PHP file.

ikcyk
commented on Thu, 26 May 2022
#!/usr/bin/sh

sites=`ls /data/www`
  
for site in $sites
do
    sed -i '{line} a\ \ \ \ $M->AddNewsHits(array("id"\ =>\ (int)\ $_GET["id"]));\n\ \ \ \ $news_info\ =\ $M->GetNewsBaseOnce(array("id"\ =>\ (int)\ $_GET["id"]),\ array("field"\ =>\ "hits"));\n\ \ \ \ echo\ "document.write(\\\"".$news_info["hits"]."\\\")"' /data/www/$site/test.php
done

The above script will insert the following code after the {line} line. Note that the whole {line} represents the number of lines, and don't leave {}. When you replace it, it is like this: sed - I'10 ahhh '/ path / to / file, which means to insert HHH on another line after line 10

lock This question has been locked and the reply function has been disabled.