0

I am trying to run a php script and do a loop. This is the code I use in terminal and it works:

for i in {1..60}; do printf "file '%s'\n" video.mp4 >>list.txt; done

However, if I try to call it in my php script as shell_exec():

shell_exec('for i in {1..60}; do printf "file '%s'\n" video.mp4 >>list.txt; done');

I get error:

PHP Parse error: syntax error, unexpected ''\n" video.mp4 >>list.txt; don' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')' in /home/vagrant/Code/play/index.php on line 58

Parse error: syntax error, unexpected ''\n" video.mp4 >>list.txt; don' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')' in /home/vagrant/Code/play/index.php on line 58

senty
  • 12,385
  • 28
  • 130
  • 260

1 Answers1

0

You must escape single quotes.

shell_exec('for i in {1..60}; do printf "file \'%s\'\n" video.mp4 >>list.txt; done');
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • Thanks a lot for that, the syntax error is gone now, however I run into another problem now. It is not looping 60 times now, instead it is looping 2 times. If I try putting 200 times, it loops 3 times. Do you have any clue? As I am in php, should i perform this task with php or is it slower? – senty Mar 18 '17 at 04:04
  • It is better to use bash script for looping rather than PHP `shell_exec`. Please refer to this http://stackoverflow.com/questions/23777992/loops-with-php-shell-exec-use-php-for-or-bash-for-do-done – Sahil Gulati Mar 18 '17 at 04:23