Storing and recalling script variables in a config file. What is the best way?
I am a newbie at making bash scripts, but I was wondering if it is possible to store, recall, and modify variables for use in bash scripts inside of a config file. For example, you could have a 10 line file that stores 10 variables, and when executing the script it could get the needed variables out of that file, and then write any changes afterwards.
My reasoning is that while I don't know all the intricacies, I'm pretty sure variables get lost after reboots, or when ran by different users, or if the command is ran in a subshell, etc. This way they would basically be permanent until changed, as well as easy to view all at once. A couple examples for their use would be If you want to keep track of how many times a script was run, it could basically go (in improper syntax) Code:
"var4=line 4 of config file; echo script has been ran $var4 times; write var4+1 to config file" Code:
var6= line 6 of conf file; mkdir directory$var6; write var6+1 back to config file. I'm actually pretty sure I could get this working with sed, but being such a newbie, I figured I would ask if this is the most efficient way to do this, or if there is perhaps a much better way to store and access "permanent until changed" variables. Thanks |
I'd read the file into an array using one of the examples here https://stackoverflow.com/questions/...-into-an-array or here are some of my notes using mapfile
Code:
# mapfile input from file Bash arrays: http://wiki.bash-hackers.org/syntax/arrays HTH |
That mapfile is a nice builtin. Never heard of it before. The only thing lacking is that you don't know what the values actually represent as you would have in a "normal" config file like ROOT="/".
If you work in bash only content reading things is easy. You just create a file with all the variable declarations and then source it. Quote:
The writing of the config file is a bit messy in BASH. Either use sed like you said or recreate the whole config file with an echo statement. Quote:
|
Quote:
|
All times are GMT -5. The time now is 09:54 AM. |