LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Read variables from a text file in Bash (https://www.linuxquestions.org/questions/linux-general-1/read-variables-from-a-text-file-in-bash-511760/)

jakev383 12-18-2006 08:22 PM

Read variables from a text file in Bash
 
I have a config file that gives sub config files in this format:
-c config1 -c config2 -c config3 -c config4
I'm trying to read these files and do a simple check to see if specific text exists within these file (I know, I could cat the whole dir and grep it, but this is what I have in my head to accomplish this.....).
Anyway, I want to read in the file and split the sub config files out, to be processed in a loop. So I read in the main config, and from there awk out the sub-config files and process them in a loop. Can someone get me bumped in the right direction?
Thanks in advance. And no, this is not homework. :)

xowl 12-18-2006 08:32 PM

Actually, if there are less than 10 of these tags, you can do it easily:
Code:

file1="cat MainConfig | gawk -F' -c ' '{print $1}'"
file2="cat MainConfig | gawk -F' -c ' '{print $2'"
file3="cat MainConfig | gawk -F' -c ' '{print $3}'"
file4="cat MainConfig | gawk -F' -c ' '{print $4}'"
file5="cat MainConfig | gawk -F' -c ' '{print $5}'"
file6="cat MainConfig | gawk -F' -c ' '{print $6}'"
file7="cat MainConfig | gawk -F' -c ' '{print $7}'"
file8="cat MainConfig | gawk -F' -c ' '{print $8}'"
file9="cat MainConfig | gawk -F' -c ' '{print $9}'"

files="$file1 $file2 $file3 $file4 $file5 $file6 $file7 $file8 $file9"

If you need more than 10 of those files, you can put it in more lines

Hope this helps

LinX

unSpawn 12-18-2006 08:32 PM

Getting them in an array would be easier because if you have confarray=(-c config1 -c config2 -c config3 -c config4), then you can get confarray=${confarray[@]//-c/}. If you're adamant you need to loop|awk, then why not while read; do case "$arg" in -c);; *) test -f $arg;; esac ?

jakev383 12-19-2006 07:57 AM

Thanks for both replies. I plan on using this script on a couple different systems, so I'm not sure how many args will be on each machine. Maybe 1 on machine-a, and 13 on machine-b.
An array would be nice. I'm not stuck in any one form (other than a Bash script).
I want to finish a script that will read in the various sub-config file from the main config file. That way I can perform another action in the loop on the sub-config file. I have the rest of it pretty much done, just need to be able to read in the sub-configs for a loop purpose.
So I read in example.conf, which has the following in it:
-c sub1.conf -c sub2.conf -c sub3.conf -c sub4.conf
Now I want to perform (as an example):
perl -pi -e 's/what_you're_looking_for/what_change_to/' sub1.conf
perl -pi -e 's/what_you're_looking_for/what_change_to/' sub2.conf
perl -pi -e 's/what_you're_looking_for/what_change_to/' sub3.conf

And so on.
Thanks again!

unSpawn 12-20-2006 06:50 AM

Check these out if you didn't already:
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/abs/html/

jakev383 12-20-2006 07:29 AM

Yeah, I did, thanks. I ended up just putting a variable in the head of the script:
CFGS="file1.conf file2.conf file3.conf" # and so on, depending on the machine

And then doing a loop based on that:

for process in ${CFGS} ; do
whatever()
done

And guess that'll have to work for now. I'd still like to know how to read that, though.


All times are GMT -5. The time now is 07:29 AM.