【问题标题】:converting a linux shell script command into php将 linux shell 脚本命令转换为 php
【发布时间】:2015-10-07 10:29:16
【问题描述】:

我面临的问题可能有一个简单的解决方案,但由于我不是 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

我的 php 尝试:

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

非常欢迎提出建议...谢谢

更新

我已经从我被标记为可能重复的问题中阅读了这个问题,我从中了解到我的“i”必须有一个引用,对于 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 linux shell


【解决方案1】:

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

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

输出: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);
?>

【讨论】:

  • 非常感谢,我不得不对代码做一些小的调整,但它确实有效,再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
相关资源
最近更新 更多