I found this little gem of BASH scripting today and wanted to share it here, mostly because I find that it fills a function that I'm constantly trying to remember, "How did I do that in the last script I wrote?"
NOTE - If you know a better way to accomplish this task, please let me know!
The following snippet of code will let you loop through a delimited text file (commas, semi-colons, tab, etc.) and assign the values to variables for scripting goodness. For this example, assume that I'm using the following source file: MyIps.csv
192.168.1.1,My Linksys
192.168.1.2,My Server
192.168.1.3,My Desktop
192.168.1.4,My Laptop
Using this as a source file, this is the script that will let me loop through the file as though it were a multi-dimensional associative array:
#!/bin/bash
OLD_IFS=$IFS
IFS=','
while read ip desc # these are the various items in the MyIps.csv
do
echo $ip' - '$desc
done < MyIps.csv
IFS=$OLD_IFS
Obviously, if your source file uses a delimiter other than a comma, you'll have to modify the script to accommodate for that.
Cheers!
Cheers!


0 comments:
Post a Comment