bash - 在 bash 子 shell 中确认

标签 bash ack

我想“在 bash 循环中”搜索多个文件。

假设我在 bash 循环中执行 ack 搜索:

#!/bin/bash

seq 3 | while read i
do
    test=`ack root /etc/passwd`
    echo $test
done

打印一个空行。

在哪里

#!/bin/bash

seq 3 | while read i
do
    # test=`ack root /etc/passwd`
    echo $test
done

打印 3 个空行。

如果我只从 bash 运行一条命令,它就可以工作:

ack root /etc/passwd

这也有效:

$ test=`ack root /etc/passwd`
$ echo $test

我认为 ack 以某种方式打破了循环。

这是“问题的根源”:

ls input/* | while read scan
do
    temp=`basename "$scan"`
    baseName=${temp%.*}
    extension=${temp#*.}

    # OCR:
    abbyyocr11 -rl Russian -if "$scan" -f TextUnicodeDefaults -of "temp/$baseName.txt"

    # get data:
    firstName=` ack '^Имя\s+(.+)'      --output='$1' "temp/$baseName.txt" | sed 's/ //g'`
    middleName=`ack '^Отчество\s+(.+)' --output='$1' "temp/$baseName.txt" | sed 's/ //g'`
    lastName=`  ack '^Фамилия\s+(.+)'  --output='$1' "temp/$baseName.txt" | sed 's/ //g'`

    # copy the file with a meaningful name:
    cp --backup=numbered "$scan" "output/$lastName$firstName$middleName.$extension"

done

编辑

原来 --nofilter 选项解决了这个问题。根据 --help 消息,它强制 ack 将 stdin 视为 tty 而不是管道。我想知道这是什么意思。

最佳答案

出于某种原因,它似乎不喜欢 stdin 上的管道。 我不知道为什么会这样,但一个快速简单的解决方法是:

seq 3 | while read i
do
    test=`</dev/null ack root /etc/passwd`
    echo $test
done

或者在你的情况下

 # get data:
 {
     #....
 } </dev/null

关于bash - 在 bash 子 shell 中确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44834705/

相关文章:

bash - bash 脚本如何对后台任务执行相当于 Ctrl-C 的操作?

vim - 如何从vim中的项目中排除文件和目录(使用janus)?

grep - 如何使用 ack(最好是 ack)或 grep 在文件列表中搜索字符串?

symlink - ack 找不到符号链接(symbolic link)

bash - 如何重置 Google Cloud Shell 用户永久磁盘?

regex - bash 正则表达式中的匹配组

bash - 使用逗号分隔的 sed awk 连接两个文件

linux - 如何正确打印作为参数传递给 Bash 函数的字符串?

find - 最佳 GNU 查找替代品

jquery - 如何让 Ack 忽略 jQuery 文件?