LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-18-2006, 08:22 PM   #1
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Rep: Reputation: 31
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.

Last edited by jakev383; 12-18-2006 at 08:24 PM.
 
Old 12-18-2006, 08:32 PM   #2
xowl
Member
 
Registered: Jan 2005
Distribution: Slackware current (and others)
Posts: 188

Rep: Reputation: 30
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
 
Old 12-18-2006, 08:32 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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 ?
 
Old 12-19-2006, 07:57 AM   #4
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Original Poster
Rep: Reputation: 31
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!
 
Old 12-20-2006, 06:50 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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/
 
Old 12-20-2006, 07:29 AM   #6
jakev383
QmailToaster Developer
 
Registered: Dec 2005
Location: Burlington, NC
Distribution: CentOS, Voyage, Debian, Fedora
Posts: 220

Original Poster
Rep: Reputation: 31
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash read a given line number from text bendeco13 Programming 7 08-31-2012 03:49 PM
Help w/ script to read file and parse variables cslink23 Linux - General 18 11-26-2006 02:22 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
entering variables using 'read' command: how to blank text? adamrosspayne Linux - Newbie 1 07-04-2006 01:37 AM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration