将Linux shell脚本命令转换为php


converting a linux shell script command into php

我正面临一个问题,可能有一个简单的解决方案,但我不是一个php专家我找不到它。我通常在需要从php调用shell命令时这样做:

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

但现在我有了一个在shell上运行的命令我不能让它在php中运行,所以我想可能有一种方式来表达相同的命令但在php语言中,

命令:

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

我得到的是:

PHP Notice:  Undefined variable: i in count.php on line 7
建议非常受欢迎。由于

我已经阅读了我被标记为可能重复的问题,我从中了解到的是我的"我";必须有一个引用,对于shell命令,它有一个引用,但是当从php执行时它不起作用。

考虑到这一点,我也尝试了这个,但没有成功:

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

PHP将计算带有双引号的字符串中的所有变量,例如:

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

output: Your i is: 5

如果你想避免这种行为,使用一个简单的引号:

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

输出: Your i is: $i

像这样更新你的代码:

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