0

I'm working on the following script to parse some files for me. The script actually does what I want - I get the correct information in the correct files. It seems that the outermost for loop runs the correct number of times. The following nested loop runs more times than necessary, and the innermost loop runs infinitely.

#!/bin/bash
for f in $HOME/configtool/interfaces/*
do
     for d in $(cat $HOME/configtool/asrlist);
     do
          for n in $(cat $HOME/configtool/nodes);
          do
               grep $d $f | grep 'up\|down' >> $HOME/configtool/distill/$n
          done
     done
done

I'd like to avoid using a magic number to kill the loop if at all possible, due to scalability

Does anyone have any ideas?

1

You don't really need to loop at all: grep -f can read a file for regexes to search for. And the innermost loop is just repeating the original greps over and over and over again, but writing the same output to different files. Why not,

out=$(mktemp)
grep --no-filename --file="$HOME"/configtool/asrlist -- "$HOME"/configtool/interfaces/* | grep 'up\|down' > "$out"
for n in $(cat "$HOME"/configtool/nodes)
do
  cp "$out" "$HOME"/configtool/distill/"$n"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.