Reputation: 516
I want to break out of this loop when grep doesn't match its pattern. The "higher" goals here were:
.
#!/bin/bash
set -ex
i=1
RERUN=0
while [ ${RERUN} == 0 ]
do
((i++))
echo -n "Rerun${i}"| tee >(grep --color=always 'Rerun[123]' ; export RERUN=$?)
echo ${RERUN}
sleep 1
done
My guess why its not working is that is has to do with different shell levels
Upvotes: 0
Views: 310
Reputation: 295272
I might write this like so:
#!/bin/bash
generate_output() {
local i=0
while :; do
(( ++i ))
printf 'Rerun%s\n' "$i"
sleep 1
done
}
while IFS= read -r line; do
printf '%s\n' "$line"
[[ $line = Rerun[123] ]] || break
done < <(generate_output)
Note that there's no need to communicate between the two processes with a shared variable -- sending a SIGPIPE to generate_output
by closing the file descriptor it's writing to suffices.
Upvotes: 1
Reputation: 516
I found a solution:
#!/bin/bash
# set -x
i=0
RERUN_FILE=$(mktemp)
declare -a TESTDATA=("run" "run" "run" "last" "run" "run" "run" )
while ! $(grep -q 1 ${RERUN_FILE})
do
echo -n ${TESTDATA[$i]}| tee >(grep --color=always 'run'; echo -n $? >${RERUN_FILE})
((i++))
sleep 1
done
rm ${RERUN_FILE} # do your dishes after the meal
Upvotes: 0