How to read variables line after line
PROMOTEBUILDS=3.1.161,1.0.132
echo cat ${PROMOTEBUILDS} | while read -r line; do
egrep $line /home/rahul-optit/packages
done
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communitySince you're using bash you should use an array instead of a variable:
promotebuilds=( 3.1.161 1.0.132 )
for i in "${promotebuilds[@]}"; do
egrep "$i" /home/rahul-optit/packages
done
@Jesse_b's answer is the way to do it.
If you however have no influence on the input, you can use IFS=,
and read
to split the string and make it an array.
printf '%s' "$PROMOTEBUILDS" | IFS=, read -a promotebuilds
for i in "${promotebuilds[@]}"; do
egrep "$i" /home/rahul-optit/packages
done
Maybe I do not understand the question. For one thing, your variable appears to have only one line.
All this time I've been reading lines from a variable the same way you would from a regular file except I enclose it in quotation marks and use '<<<' instead of '<' to feed it to the while loop.
VARIABLE=$(cat somefilewithmultiplelines)
while read i ; do echo "$i" ; done <<< "$VARIABLE"