LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 10-27-2003, 01:41 PM   #1
spikylee
LQ Newbie
 
Registered: Oct 2003
Posts: 3

Rep: Reputation: 0
Question (Bash) Saving READ values in external .conf files


Hi,

Im writing a simple script in bash that echo's a menu, show's some options and then askes the user to input some value.
I capture this value in a variable and then I need to write this value in an external .conf file. Is this possible? Should I use awk to do this, or is there some easier way?
I guess already a lot of people have solved this, so could anybody explain to me what the normal way to do this is??

THanx!!
 
Old 10-27-2003, 01:44 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Echo the variable to your outfile:

echo $WHATEVER >> <something>.conf

You do need the >> instead of >.

>> appends to file
> overwrite/create file
 
Old 10-28-2003, 04:54 AM   #3
spikylee
LQ Newbie
 
Registered: Oct 2003
Posts: 3

Original Poster
Rep: Reputation: 0
Tnx!

This makes me wonder about the functionality of my script.

If I use a configuration file that already contains variables, plus comments about the variables role, and I wish to edit a variable's value but keep the comments and other values, how will my edit operation look like:

- lookup line number of existing var
- remove
- append new value
- save
?

Or is there an easier way to do this??
 
Old 10-28-2003, 07:33 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
sed and/or awk could be used for what you want.

Both have many options and can be as complex (or simple) as you want. You should read the man pages, search the net or buy a book about sed and/or awk.

O'Reilly has a good book that covers both (sed & awk)

An on-line awk tutorial/examples:
http://www.vectorsite.net/tsawk.html
http://www.novia.net/~phridge/programming/awk/
http://www.gnu.org/manual/gawk-3.1.1...mono/gawk.html

On-line sed tutorials/examples:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/tutorials/
http://sed.sourceforge.net/

There are a lot more, just feed your searchengine with the appropriate keywords :-)

How you set up your .conf file(s) depends on, among other things:

- is (human)readability important,
- does every user need his/her own .conf file.

I like the 'one line per user' approach. Which also means that you could do with only 1 .conf file. The file will be (a bit) harder to read by humans, but it's a lot easier to work with from a programming point of view (most tools in unix/linux are line driven by default).

Using the one-line-per-user approach your .conf file will look like this (or take a look at the way /etc/passwd is set up):

#username age sex
jake 22 male
jane 25 female

The space is the seperator (which you can change: see /etc/passwd, seperator is ':').

Hope this helps a bit.
 
1 members found this post helpful.
Old 10-28-2003, 07:46 AM   #5
spikylee
LQ Newbie
 
Registered: Oct 2003
Posts: 3

Original Poster
Rep: Reputation: 0
My script is a root-only script and the .conf file has to be human-readible (these are conditions of the assignment) so I'll have to use awk solution, which seems not to difficult. (Provided me keeping the statements easy )

but first some !

tnx a lot!
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash; reading values from a file km4hr Programming 16 07-28-2005 03:07 PM
Newbie: How to read/write conf/ini files? neo_in_matrix Programming 2 02-22-2005 07:26 PM
Trying to read named.conf forwarders info in bash script. scottman Programming 4 09-11-2004 10:38 PM
saving changes to apache2/conf/httpd.conf file Zaius Linux - Newbie 6 01-10-2004 12:45 AM
bash - comparing a variable to several values davee Programming 3 05-05-2003 08:26 AM


All times are GMT -5. The time now is 01:58 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration