On Mon, 18 August 2014, at 10:42 am, wraeth <wra...@wraeth.id.au> wrote:

> On Mon, 2014-08-18 at 18:54 +1000, Adam Carter wrote:
>> But this matches if grep fails both times as well as when it matches both
>> time. Any ideas?
> 
> If you don't mind using a quick loop, you could use something like:
> 
> n=0
> for f in file1.txt file2.txt file3.txt file4.txt; do
>    grep 'string' ${f} >> /dev/null && n=$[n+1]
> done
> 
> if [[ $n == 4 ]]; then
>    do_something
> fi

I've solved similar problems the same way myself, but I hope you'll forgive me 
for offering unsolicited critique on a small detail.

In the above 4 is a constant, and thus it's independent of the number of files 
being tested.

I propose addressing this with an array of the filenames.

Thus additional files can be added for testing, without manual adjustment of 
the expected total.

  files=("file1.txt" "file2.txt" "file3.txt" "file4.txt")
  n=0
  for f in ${files[@]}; do
     grep 'string' ${f} >> /dev/null && n=$[n+1]
  done

  if [[ $n == ${#files[@]} ]]; then
     do_something
  fi

Bash array syntax is a bit arcane, but at least these very useful data 
structures are available.

Stroller.


Reply via email to