find with exec : how to perform multistep conditional action on each file
I have bunch of php files in directory structure say /mylibs
I want to run a simplephp -l $file
on each php file which checks for syntax errors
find /mylibs -type f -iname "*.php" -exec php -l {} &>/dev/null \;
thats step one, the &>/dev/null eats verbose output from php (found syntax errors or not)
The php -l
returns 0 if no error is found depending upon which, I want to copy them to some other dir say /mybin
. To check if this works as expected I tried
find /mylibs -type f -iname "*.php" -ok php -l {} &>/dev/null ; echo $? \;
but this simply prints 1 on the terminal and开发者_Go百科 does not ask for confirmation (-ok acts same as -exec after interactive confirmation)
What am I doing wrong here ? is it not possible to do this?
You can use a while
loop:
find /mylibs -type f -iname "*.php" | while IFS= read -r path
do
php -l "$path" &>/dev/null
if [ $? -eq 0 ]
then
cp "$path" /mybin
fi
done
does this work for u?
find ....|xargs awk '{r=system("php -l "$0" &>/dev/null"); if(!r) system("cp "$0" /path/to/targetDir")}'
精彩评论