2

In a bash script, result of find is

/path/to/file1.nrg
/path/to/file2.nrg
/path/to/file3.nrg

i have this while loop:

process preset

processpreset ()
{
    x=$1
    # Replace , by -o -iname for file types.
    iname=" -o -iname \*."
    # Find specified files. Eval allow var prst1_in with find.
    eval "find "$fpath" -type f \( -iname \*."${prst_in[x]//,/$iname}" \) -size ${prst_lim_size[x]}" | sort | while read -r i
    do
        titles=$(HandBrakeCLI --input "$i" --scan |& grep -Po '(?<=DVD has )([0-9]+)')
        if (( $titles > 1 )); then
        echo "DVD has $titles title(s)"
        fi
    done
}

the script only echo 1 time File has 8 title(s) after it stop, when using titles="8" the loop echo for all files in folder. Can anyone point me my error please?

EDIT: what work for me, many thanks Anubhava

processpreset ()
{
    x=$1
    # Replace , by -o -iname for file types.
    iname=" -o -iname \*."
    # Find specified files. Eval allow var prst1_in with find.
    eval "find "$fpath" -type f \( -iname \*."${prst_in[x]//,/$iname}" \) -size ${prst_lim_size[x]}" | sort | while read -r i
    do

    titles="$(echo ""|HandBrakeCLI --input "$i" --scan |& grep -Po '(?<=DVD has )([0-9]+)')"
    if (( $titles > 1 )); then
      echo "DVD has $titles title(s)"
    fi
    done
}

the echo ""| fix the problem.

Eeel
  • 117
  • 7
  • But your while loop is just reading variable from stdin. – anubhava Feb 10 '14 at 21:17
  • from HandBrake Manual Log data is output to Standard Error Encode progress information (ETA, Percent Complete, Avg Encoding framerate etc) are output to Standard Output. why redirecting block the loop to continue, don't understand – Eeel Feb 10 '14 at 21:23
  • I am talking about `while read -r i` Are you just reading `i` from terminal every time loop runs? – anubhava Feb 10 '14 at 21:28
  • @anubhava i have edited with the full code, did it answer your question ? – Eeel Feb 10 '14 at 21:35
  • How many lines you get from `ind $imgpath -type f \( -iname \*.iso -o -iname \*.nrg -o -iname \*.img \)" | sort` command? – anubhava Feb 10 '14 at 21:37
  • it return 2 files, but as soon as is put `titles=$(HandBrakeCLI --input "$i" --scan |& grep -Po '(?<=DVD has )([0-9]+)')` it return only one file – Eeel Feb 10 '14 at 21:41

1 Answers1

3

ok try this script:

while read -r i
do
        echo "i is: $i"
        titles="$(echo ""|HandBrakeCLI --input "$i" --scan | grep -Po '(?<=DVD has )([0-9]+)')"
        if (( titles > 1 )); then
           echo "DVD has $titles title(s)"
        fi
done < <(find "$imgpath" -type f \( -iname \*.iso -o -iname \*.nrg -o -iname \*.img \) | sort)
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • no unfortunatly it doesn't work, i have added the full function in my first post since it seems to be more complicated than i thought at the beginning. (there is a missing quote before find in your previous post). thanks for your time – Eeel Feb 10 '14 at 22:05
  • Thoses post seems to relate a similar problem [link](http://stackoverflow.com/questions/2708097/preventing-a-child-process-handbrakecli-from-causing-the-parent-script-to-exit) [link](http://stackoverflow.com/questions/2708097/preventing-a-child-process-handbrakecli-from-causing-the-parent-script-to-exit) – Eeel Feb 10 '14 at 22:07
  • Ah corrected that typo but your code appears to be far more complicated than earlier version. – anubhava Feb 10 '14 at 22:08
  • Have another link about same issue [link](http://stackoverflow.com/questions/5549405/shell-script-while-read-loop-executes-only-once) this is from far over my skill – Eeel Feb 10 '14 at 22:18
  • Many thanks, with some modification it work as intended, edited my first post. Don't understand how this `echo ""` can help. `(( titles > 1 ))` doesn't work for me, have to use `$titles` – Eeel Feb 10 '14 at 22:58