1 |
On Mon, 18 August 2014, at 10:42 am, wraeth <wraeth@×××××××××.au> wrote: |
2 |
|
3 |
> On Mon, 2014-08-18 at 18:54 +1000, Adam Carter wrote: |
4 |
>> But this matches if grep fails both times as well as when it matches both |
5 |
>> time. Any ideas? |
6 |
> |
7 |
> If you don't mind using a quick loop, you could use something like: |
8 |
> |
9 |
> n=0 |
10 |
> for f in file1.txt file2.txt file3.txt file4.txt; do |
11 |
> grep 'string' ${f} >> /dev/null && n=$[n+1] |
12 |
> done |
13 |
> |
14 |
> if [[ $n == 4 ]]; then |
15 |
> do_something |
16 |
> fi |
17 |
|
18 |
I've solved similar problems the same way myself, but I hope you'll forgive me for offering unsolicited critique on a small detail. |
19 |
|
20 |
In the above 4 is a constant, and thus it's independent of the number of files being tested. |
21 |
|
22 |
I propose addressing this with an array of the filenames. |
23 |
|
24 |
Thus additional files can be added for testing, without manual adjustment of the expected total. |
25 |
|
26 |
files=("file1.txt" "file2.txt" "file3.txt" "file4.txt") |
27 |
n=0 |
28 |
for f in ${files[@]}; do |
29 |
grep 'string' ${f} >> /dev/null && n=$[n+1] |
30 |
done |
31 |
|
32 |
if [[ $n == ${#files[@]} ]]; then |
33 |
do_something |
34 |
fi |
35 |
|
36 |
Bash array syntax is a bit arcane, but at least these very useful data structures are available. |
37 |
|
38 |
Stroller. |