-   Linux - Newbie (
-   -   (Bash) Saving READ values in external .conf files (

spikylee 10-27-2003 01:41 PM

(Bash) Saving READ values in external .conf files

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??


druuna 10-27-2003 01:44 PM

Echo the variable to your outfile:

echo $WHATEVER >> <something>.conf

You do need the >> instead of >.

>> appends to file
> overwrite/create file

spikylee 10-28-2003 04:54 AM


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??

druuna 10-28-2003 07:33 AM

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:

On-line sed tutorials/examples:

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.

spikylee 10-28-2003 07:46 AM

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 :D )

but first some :study:!

tnx a lot!

All times are GMT -5. The time now is 08:15 PM.