I am having a text file
best = 100
genre = 75
group = 53
.
.
and so on
I need to read those values (100,75,53,...) from my shell script. I have to do the same function for all the variables. So in my script i used for loop to do the same.
for {
a=best
b=100
}
the value "a" and "b" must change for each iteration.
This is one approach
while read LINE
do
a=`echo $LINE | cut -d "=" -f1`
b=`echo $LINE | cut -d "=" -f2`
echo "a $a b $b"
done<temp.txt
If you can discuss what you do after fetching a and b, then our experts can provide more efficient methods for your entire script.
A better/faster way:
#!/usr/bin/ksh
IFS=' = '
while read i
do
set "$i"
a=$1
b=$2
echo $a $b
done < t123
And make sure to change IFS if further processing uses this variable...
Another way that saves a few processes:
$ cat x.dat
best = 100
genre = 75
group = 53
best = 101
genre = 76
group = 54
best = 102
genre = 77
group = 55
best = 103
genre = 78
group = 56
$ cat x
#!/bin/ksh
ctr=0
while read a equalsign b
do
((ctr+=1))
print "line $ctr: $a $b"
done < x.dat
exit 0
$ x
line 1: best 100
line 2: genre 75
line 3: group 53
line 4: best 101
line 5: genre 76
line 6: group 54
line 7: best 102
line 8: genre 77
line 9: group 55
line 10: best 103
line 11: genre 78
line 12: group 56
$