bash reading file using IFS read loop

Sep 27, 08:26 AM

the problem was trying to get the variables to process outside of the subshell, otherwise the value was not preserved!

https://unix.stackexchange.com/questions/16990/using-data-read-from-a-pipe-instead-than-from-a-file-in-command-options

https://stackoverflow.com/questions/69339514/saving-values-in-bash-shell-variables-while-using-tee

  1. cat test.txt:
    first example line one
    first example line two
    first example line three
    second example line one
    second example line two
  1. test.bsh:
    first=0;
    second=0;
    #while read -r line; do
    while IFS=’‘ read -r line; do [[ $line *'first example'* ]] && ((++first)) [[ $line ‘second example’ ]] && ((++second))
    done < <(cat test.txt;) ## notice passing in command results using <() syntax.

echo $first ## should display 3
echo $second ## should display 2

Mark Edwards

,

---

Commenting is closed for this article.

---