Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec with grep using a variable passed?

I am sure there is an easy answer , but i searched and could not find.

I am basically trying to pass a variable to exec grep into a file

so

exec("snmpget -v 2c -c public $host $oid_model | cut -d':' -f4 | sed -e 's/^[ ]*//'", $model);
exec("grep $model /home/user/test.txt  |  cut -d':' -f1 | sed -e 's/^[ ]*//'", $new_model);

when printing my $model i get the output, when trying $new_model it does not.

like image 461
Luis Avatar asked Aug 11 '26 16:08

Luis


1 Answers

Try to quote the matching pattern in grep command:

exec("grep '".$model[0]."' /home/user/test.txt  |  cut -d':' -f1 | sed -e 's/^[ ]*//'", $new_model);
like image 103
anubhava Avatar answered Aug 13 '26 06:08

anubhava