Page 1 of 1
Bash Script input variables from file
Posted: Fri Sep 29, 2006 7:21 pm
by facets
Hi All,
How would I assign variables in a bash script from a text file with 2 strings per line separated by a : ?
This code is echoing the code back, but I have no idea how to assign the variables.
Code: Select all
#!/bin/bash
while read a ; do
echo $a
done < file.list
Any pointers would be rad!
Will./
Posted: Sat Sep 30, 2006 7:30 am
by Weirdan
what variables are you trying to assign? what's in the file you're reading?
Posted: Sat Sep 30, 2006 7:52 am
by facets
currently just inputting text.
the test file looks like this.
- One,Two
Three,Four
Five,Six
So I wish to (eventually) loop through these items so
Loop1 >
Var1 = One
Var2 = Two
Loop2 >
Var1 = Three
Var2 = Four
etc..
Posted: Sat Sep 30, 2006 8:51 am
by Chris Corbyn
Use awk with a "," field separator to split each line, then get your variables from there

Posted: Sun Oct 01, 2006 3:59 am
by facets
Here's what I ended up with.. Thanks all for help.
If someone wants to help me with an awk one liner.. be my guest
Code: Select all
#!/bin/bash
while IFS=" " read name pass
do
useradd "$name" -p "$pass"
done < mkuser.txt
exit 0
tks, Will./
Posted: Tue Oct 03, 2006 4:37 am
by ibbo
you can assign from file via awk like this
NAME=`awk -F, '{print $1}' /path/to/data`
PASS=`awk -F, '{print $2}' /path/to/data`
But this has problems when you have multiple entries in your file. Your method works good enough.
Ibbo