cwheeler33 cwheeler33 - 1 year ago 108
Bash Question

Bash script: how to make two variables from a string?

I have a list file that contains server names and IP addresses.
How would I go about reading each line, and separating it into two variables that will be used to complete other commands?

Sample in MyList:

intended script

while read line
echo $line #I see a print out of the hole line from the file
"how to make var1 ?" #want this to be the hostname
"how to make var2 ?" #want this to be the IP address
echo $var1
echo $var2
done < $MyList

Answer Source

Just pass multiple arguments to read:

while read host ip
    echo $host
    echo $ip

If there is a third field you don't want to read into $ip, you can create a dummy variable for that:

while read host ip ignored
    # ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download