while loop redirect command into it

while ......; do
something
done < file

i'd like to

while ...;do
something
done < (cat file | grep something)

The latter doesn't work and I'd really appreciate if someone can advise on the correct syntax.
lolaferrariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omarfaridCommented:
try this

 grep something file| while ...;do
something
done
0
Steven CarnahanNetwork ManagerCommented:
Wouldn't this work also?

while ...;do
something
done <<< grep something

Open in new window

0
Gerwin Jansen, EE MVETopic Advisor Commented:
>> done < (cat file | grep something)

The  (cat file | grep something) part must be a file, which it is not.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

TintinCommented:
You need to do

grep string file | while read line
do
    ....
done

Open in new window

0
lolaferrariAuthor Commented:
This logic only works on /etc/passwd file but I use the same logic on another file it only works for the first line in the file.  It runs correctly for the first line in the file and then grinds to a halt

input file looks like this

<hostname>,<OS>,<comment>,<user>
ie
fonzy,SunOS 5.10,NOT-CORRECT,izywizy

gonzo,HP-UX  11.1,NOT-CORRECT,izywizy

bigbird,RHEL,NOT-CORRECT,izywizy


tried
....
 while IFS=',' read f1 f2 f3 f4;do
OS=$(echo $f2 | awk '{print $1}')
echo "$f1 $f2 $f3 $f4"
if [[ "$f2" == "RHEL" ]]; then
do something $OS $f4
else
if if [[ "$f2" == "SunOS" ]]; then
do something $OS $f4
else
if ... [[....
fi
fi
fi
done < inputfile

I was reading that read -r might help but it doesn't work here!

The above example works perfectly but it only reads the first line in the file. I also tried changing the IFS to \n to try to read another line below but it still only reads one line in the file. I also tried to putting IFS=$OIFS after while ... read but still no change

OIFS=$'\n'
while IFS=',' read f1 f2 f3 f4;do
OS=$(echo $f2 | awk '{print $1}')
echo "$f1 $f2 $f3 $f4"
if [[ "$f2" == "RHEL" ]]; then
do something $OS $f4
else
if if [[ "$f2" == "SunOS" ]]; then
do something $OS $f4
else
if ... [[....
fi
fi
fi
IFS=$OIFS
done < inputfile
0
Gerwin Jansen, EE MVETopic Advisor Commented:
If I do this:

#!/bin/bash
IFS=","
while read f1 f2 f3 f4
do
	OS=$(echo ${f2} | awk '{print $1}')
	echo "${f1} ${f2} ${f3} ${f4}"
	if [[ "${f2}" == "RHEL" ]]
	then
		echo "something ${OS} ${f4}"
	fi
done < inputfile.txt

Open in new window

with this inputfile:

fonzy,SunOS 5.10,NOT-CORRECT,izywizy
gonzo,HP-UX  11.1,NOT-CORRECT,izywizy
bigbird,RHEL,NOT-CORRECT,izywizy

I get this output:

fonzy SunOS 5.10 NOT-CORRECT izywizy
gonzo HP-UX  11.1 NOT-CORRECT izywizy
bigbird RHEL NOT-CORRECT izywizy
something RHEL izywizy

Seems OK to me.
0
ozoCommented:
There are several syntax errors in the above script, but with the following changes, it is able to read every line in my sample inputfile

while IFS=',' read f1 f2 f3 f4;do
OS=$(echo $f2 | awk '{print $1}')
echo "$f1 $f2 $f3 $f4"
if [[ "$f2" == "RHEL" ]] ; then
echo do something $OS $f4
else
echo else
if [[ "$f2" == "SunOS" ]] ; then
echo do something $OS $f4
else
echo else
fi
fi
done < inputfile
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nociSoftware EngineerCommented:
be aware that piped commands run in a subshell. So setting variables in the while loop will not be visible outside of the loop.
0
lolaferrariAuthor Commented:
The extremely fast feedback a couple of days ago didn't go unnoticed. Thank you very much.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Not complaining but did my comment above not work for you?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.