0

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

1
  • 1
    Just concatenate it like: 'echo "' . $hour . ' ' . $mins . ' Commented Jun 19, 2014 at 11:47

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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 -");

Comments

1

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));

Comments

1

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 -');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.