3

I want to search a number of files "in a bash loop".

Suppose I exec an ack search in a bash loop:

#!/bin/bash

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

prints one empty line.

Where as

#!/bin/bash

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

prints 3 empty lines.

If I ran just that one command from the bash it works:

ack root /etc/passwd

This also works:

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

I think ack somehow breaks the loop.

Here's "the origin of the problem":

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

Edit

Turns out --nofilter option solves it. According to --help message it forces ack treat stdin as tty as opposed to pipe. I wonder what this means.

Adobe
  • 12,967
  • 10
  • 85
  • 126

1 Answers1

2

It looks like it doesn't like a pipe on stdin for some reason. I don't know why it does that, but a quick and easy fix would be:

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

or in your case

 # get data:
 {
     #....
 } </dev/null
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • Thanks. After reading your answer, I re-read the `--help` message and found out that `--nofilter` option works for my usecase. – Adobe Jun 30 '17 at 06:06