Marc Ster
Marc Ster

Reputation: 2316

php exec an echo with variables and string

I want to to exchange "30" and "16" with two variables "hour" and "mins" in a php exec function

$hour = 30;
$mins = 16;
exec('echo "30 16 * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');

and changed it like that:

exec('echo "$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');

How can i get this right? Thank you

Upvotes: 0

Views: 1023

Answers (4)

Arun Magar
Arun Magar

Reputation: 101

You can do this using concat string

exec('echo "'.$hour.' ' .$mins.' * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');exec('echo "$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');

Upvotes: 1

mpyw
mpyw

Reputation: 5744

I recommend you sprintf. That does not lose readability.

$format = 'echo "%d %d * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -';
exec(sprintf($format, $hour, $mins));

Upvotes: 1

Jim
Jim

Reputation: 22646

Variables inside single quoted strings are not populated. Try either

exec('echo "'.$hour.' '.$mins.' * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');

or

exec("echo \"$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1\" | crontab -");

PHP String Documentation

Upvotes: 3

Barmar
Barmar

Reputation: 780714

Variables aren't expanded inside single-quoted strings, only inside double-quoted strings. So switch your quotes around.

exec("echo '$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1' | crontab -");

Upvotes: 2

Related Questions