I am writing a script to let me know the database login info for each PHP-based web site on a server that I am migrating. The login information is usually done either like this:$servername='localhost';$database_username='someName';$database_password='p@ssw0rd';$database_name='someDatabase';mysql_connect($servername, $database_username, $database_password);mysql_select_db($database_name);Or like this:$pdo = new PDO(mysql: host={$servername}; dbname={$$database_name}, $database_username, $database_password);The script is searching for each line which contains one of the PHP connection functions:grep -irE mysql_connect\|mysql_select_db\|new PDO *Now, how can I get each string that begins with $ and ends with a non-alphanumeric? Additionally, if no ; character is found on the screen I would like to be notified so that I could handle it manually, as some coders might do this:$pdo = new PDO(mysql: host={$servername};                dbname={$$database_name},                $database_username,                $database_password               );After I get these tokens (PHP variables), I'll grep the file again for the tokens in hope to get their values;
How to get the arguments to a php function via grep?
shell script;scripting;bash script
I am not really sure what you are trying to do here, but you can grep all strings that begin with $ and end with a non-alphanumeric character with this:grep -oP '\$.+?\W' In response to your comment, how about something like this:for f in `find . -name *.php`; do \  echo ----- $f -----;  for s in $(grep -iE mysql_connect|mysql_select_db|new PDO $f |      grep  -oP '\$\w+?\W' | sed 's/.$//' | sort | uniq); do \       grep -m 1 $s $f;  done; doneThis scriptlet will find all .php files, grep each of them for the variables you are interested in and then print the first instance of each variable in the file it was found in. For example, I created 2 .php files, aa.php and bb.php, their contents are:$ cat aa.php $servername='localhost';$database_username='someName';$database_password='p@ssw0rd';$database_name='someDatabase';mysql_connect($servername, $database_username, $database_password);mysql_select_db($database_name);$ cat bb.php $servername='localhost2';$database_username='some2Name';$database_password='p@ssw0rd2';$database_name='someDatabase2';$pdo = new PDO(mysql: host={$servername}; dbname={$$database_name}, $database_username, $database_password);The output of the script above is:----- ./bb.php -----$database_name='someDatabase2';$database_password='p@ssw0rd2';$database_username='some2Name';$pdo = new PDO(mysql: host={$servername}; dbname={$$database_name}, $database_username, $database_password);$servername='localhost2';----- ./aa.php -----$database_name='someDatabase';$database_password='p@ssw0rd';$database_username='someName';$servername='localhost';