1837240 Members
4796 Online
110115 Solutions
New Discussion

Re: Shell script

 
Fenil Manek_4
Occasional Contributor

Shell script

I am writing a shell script in which I am using as for loop
as below

for entry in `cat /tmp/sa`
do
echo $entry
done

The content of file /tmp/sa is
08:06:03 10 20 10 20
09:07:03 20 30 20 20
10:07:03 20 10 20 10

However when I do echo $entry in script
it is showing single field 08:06:03 instead
of entire line
08:06:03 10 20 10 20

Is there any Ouput field separator ins shell
so that my for loop variable fetches
a line

Regards
Nitin
6 REPLIES 6
Stefan Farrelly
Honored Contributor

Re: Shell script

Add 1 line before you script;

IFS=""

Then it works.

IFS=""
for entry in $(cat /tmp/sa)
do
echo $entry
done
Im from Palmerston North, New Zealand, but somehow ended up in London...
Massimo Bianchi
Honored Contributor

Re: Shell script

Hi,
you can change you script like this

while read TEXT
do
echo $TEXT
done << /tmp/sa

HTH,
Massimo


V.Tamilvanan
Honored Contributor

Re: Shell script

Hi,
Easiest way is
-------------
cat /tmp/sa|while read line
do
echo $line
sleep 3
done
----------------

HTH

-tamil
john korterman
Honored Contributor

Re: Shell script

Hi,
in order to get all arguments from the line, you need to use quotes, e.g.:

for entry in "`cat /tmp/sa`"
do
echo "$entry"
done

a better looking notation with same effect:
for entry in "$(cat ./infile)"

regards,
John K.

it would be nice if you always got a second chance
V. V. Ravi Kumar_1
Respected Contributor

Re: Shell script

hi,

while read STR
do
echo $STR
done < /tmp/sa



Regards
Never Say No
John Meissner
Esteemed Contributor

Re: Shell script

you could do it like this:

cat file |
while read line
do
echo $line
done

you could also do it like this if you wanted only specific fields:

cat file |
whle read a b c d e
do
echo $a $b $e
done
All paths lead to destiny