title: converting a linux shell script command into php
body: <p>I'm facing a problem that probably has an easy solution, but as I'm not a php expert I can not find it. I usually do this when I have to call a shell command from php:</p>
  <pre><code>cmd = &quot;convert file.pdf image.jpg&quot;;
  shell_exec($cmd);
  </code></pre>
  <p>But now I have a command that works on the shell that I can not make it run from the php, so I think there is probably a way to enunciate the same command but in php language,</p>
  <p><em>the command:</em></p>
  <pre><code>for i in $(seq --format=%3.f 0 $nf); do echo doing OCR on page $i; tesseract '$imgdir/$imgdir-$i.ppm' '$imgdir-$i' -l eng; done
  </code></pre>
  <p><em>my php try:</em></p>
  <pre><code>&lt;?php
  $imgdir = &quot;1987_3&quot;;
  $nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
  $cmd = &quot;for i in $(seq --format=%3.f 0 $nf); do echo doing OCR on page $i; tesseract '$imgdir/$imgdir-$i.ppm' '$imgdir-$i' -l eng; done&quot;
  shell_exec($cmd);
  ?&gt;
  </code></pre>
  <p><em>What I get is:</em></p>
  <pre><code>PHP Notice:  Undefined variable: i in count.php on line 7
  </code></pre>
  <p>suggestions are very welcomed...  thanks</p>
  <p><strong>UPDATE</strong></p>
  <p>I have read the question from the one I'm marked as possible duplicated, and what I understood from that was that my &quot;i&quot; have to have a reference, which, for a shell command, it has, but it does not work when executed from php.</p>
  <p>In regard of that I also tried this unsuccessfuly:</p>
  <pre><code>&lt;?php
  $imgdir = &quot;1987_3&quot;;
  $nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
  $cmd =&quot;seq --format=%3.f 0 $nf&quot;;
  $i = shell_exec($cmd);
  $cmd = &quot;tesseract 'filesup/$imgdir/$imgdir-$i.jpg' 'filesup/$imgdir/$imgdir-$i' -l eng; done&quot;;
  shell_exec($cmd);
  ?&gt;
  </code></pre>