Andrés Chandía
Andrés Chandía

Reputation: 1049

converting a linux shell script command into php

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:

cmd = "convert file.pdf image.jpg";
shell_exec($cmd);

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,

the command:

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

my php try:

<?php
$imgdir = "1987_3";
$nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
$cmd = "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"
shell_exec($cmd);
?>

What I get is:

PHP Notice:  Undefined variable: i in count.php on line 7

suggestions are very welcomed... thanks

UPDATE

I have read the question from the one I'm marked as possible duplicated, and what I understood from that was that my "i" have to have a reference, which, for a shell command, it has, but it does not work when executed from php.

In regard of that I also tried this unsuccessfuly:

<?php
$imgdir = "1987_3";
$nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
$cmd ="seq --format=%3.f 0 $nf";
$i = shell_exec($cmd);
$cmd = "tesseract 'filesup/$imgdir/$imgdir-$i.jpg' 'filesup/$imgdir/$imgdir-$i' -l eng; done";
shell_exec($cmd);
?>

Upvotes: 3

Views: 2299

Answers (1)

Halayem Anis
Halayem Anis

Reputation: 7785

PHP will evaluate all variables inside a string with double quotes, example:

<?php
    $i=5;
    echo "Your i is: $i";
?>

output: Your i is: 5

If you want to avoid this behaivor, use a simple quote:

<?php
    $i=5;
    echo 'Your i is: $i';
?>

output: Your i is: $i

Update your code like this:

<?php
    $imgdir = "1987_3";
    $nf = count(new GlobIterator('filesup/'.$imgdir.'/*'));
    $cmd = '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';    
    shell_exec($cmd);
?>

Upvotes: 3

Related Questions