Read from a file and then use command "set"

Read from a file and then use command "set"

am 01.02.2008 05:41:09 von lalo

Hi all,

I have a small problem :

I have a file with two columns :

ipaddress1 11.22.33.44
netmask1 255.255.255.0
broadcast1 255.255.255.255
gateway1 11.22.33.1
ipaddress2 55.66.77.88
netmask2 255.255.255.0
broadcast2 255.255.255.255
gateway2 55.66.77.1


Now I just want to read from that file, assign an variable to each
of them, preferably in such a way :

ip1=11.22.33.44
netmask1=255.255.255.0
gateway1=11.22.33.1
broadcast1=255.255.255.255
ipaddress2=55.66.77.88
netmask2=255.255.255.0
broadcast2=255.255.255.255
gateway2=55.66.77.1

And then to set each of them such as
set $ip1
set $netamsk1
..
..
..
set $gateway2


And then to print them so that
$ip1 be printed as 11.22.33.44
$netmask1 to be printed as 255.255.255.0
..
..
..$gateway2 to be printed as 55.66.77.1




I did similar to this but it should be a better and easier way

Thank you in advance,
Lalo


#======================================================
#!/bin/sh
exec 3< infile
foo=1
while read line1 <&3 # Read infile line-by-line
do

set -- $line1

case "$foo" in
1)
ipaddress=$2
echo "IP Address is : $ipaddress"

;;
2)
Netmask=$2
echo "Netmask is : $Netmask"

;;
3)
Broadcast=$2
echo "Broadcast is : $Broadcast"

;;
4)
Gateway=$2
echo "Gateway is : $Gateway"
set $Gateway

;;
5)
echo "Bad command, your choices are: ip , netmask
or brdcst"
;;

1
esac
foo=$(($foo+1))
set $ipaddress
set $Netmask
set $Broadcast
set $Gateway
done

#=========================================================== ====

Re: Read from a file and then use command "set"

am 01.02.2008 06:28:26 von Ed Morton

On 1/31/2008 10:41 PM, Lalo wrote:
>
> Hi all,
>
> I have a small problem :
>
> I have a file with two columns :
>
> ipaddress1 11.22.33.44
> netmask1 255.255.255.0
> broadcast1 255.255.255.255
> gateway1 11.22.33.1
> ipaddress2 55.66.77.88
> netmask2 255.255.255.0
> broadcast2 255.255.255.255
> gateway2 55.66.77.1
>
>
> Now I just want to read from that file, assign an variable to each
> of them, preferably in such a way :
>
> ip1=11.22.33.44
> netmask1=255.255.255.0
> gateway1=11.22.33.1
> broadcast1=255.255.255.255
> ipaddress2=55.66.77.88
> netmask2=255.255.255.0
> broadcast2=255.255.255.255
> gateway2=55.66.77.1
>
> And then to set each of them such as
> set $ip1
> set $netamsk1
> .
> .
> .
> set $gateway2
>
>
> And then to print them so that
> $ip1 be printed as 11.22.33.44
> $netmask1 to be printed as 255.255.255.0
> .
> .
> .$gateway2 to be printed as 55.66.77.1
>
>
>
>
> I did similar to this but it should be a better and easier way
>
> Thank you in advance,
> Lalo
>
>
> #======================================================
> #!/bin/sh
> exec 3< infile
> foo=1
> while read line1 <&3 # Read infile line-by-line
> do
>
> set -- $line1
>
> case "$foo" in
> 1)
> ipaddress=$2
> echo "IP Address is : $ipaddress"
>
> ;;
> 2)
> Netmask=$2
> echo "Netmask is : $Netmask"
>
> ;;
> 3)
> Broadcast=$2
> echo "Broadcast is : $Broadcast"
>
> ;;
> 4)
> Gateway=$2
> echo "Gateway is : $Gateway"
> set $Gateway
>
> ;;
> 5)
> echo "Bad command, your choices are: ip , netmask
> or brdcst"
> ;;
>
> 1
> esac
> foo=$(($foo+1))
> set $ipaddress
> set $Netmask
> set $Broadcast
> set $Gateway
> done
>
> #=========================================================== ====

Yes. Just do:

awk '{printf "%s is : %s\n",$1,$2}' infile

If you really NEED "variables" for some later operation, then instead just
populate an array, e.g.:

awk '{a[$1]=$2} END{for (i in a) printf "%s is : %s\n",i,a[i]}' infile

Otherwise, tell us what you're really trying to do as the code you posted
doesn't make sense on it's own.

Ed.

Re: Read from a file and then use command "set"

am 01.02.2008 12:12:52 von PK

Lalo wrote:

> Hi all,
>
> I have a small problem :
>
> I have a file with two columns :
>
> ipaddress1 11.22.33.44
> netmask1 255.255.255.0
> broadcast1 255.255.255.255
> gateway1 11.22.33.1
> ipaddress2 55.66.77.88
> netmask2 255.255.255.0
> broadcast2 255.255.255.255
> gateway2 55.66.77.1
>
>
> Now I just want to read from that file, assign an variable to each
> of them, preferably in such a way :
>
> ip1=11.22.33.44
> netmask1=255.255.255.0
> gateway1=11.22.33.1
> broadcast1=255.255.255.255
> ipaddress2=55.66.77.88
> netmask2=255.255.255.0
> broadcast2=255.255.255.255
> gateway2=55.66.77.1
>
> And then to set each of them such as
> set $ip1
> set $netamsk1
> .
> .
> .
> set $gateway2
>
>
> And then to print them so that
> $ip1 be printed as 11.22.33.44
> $netmask1 to be printed as 255.255.255.0
> .
> .
> .$gateway2 to be printed as 55.66.77.1

It's not clear what you want to do, however

eval `awk '{ print $1 "=" $2 }' file.txt`

leaves you in an environment where the variables are set and can be used
(and printed of course).

But maybe I did not understand correctly.