LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Storing and recalling script variables in a config file. What is the best way? (https://www.linuxquestions.org/questions/linux-newbie-8/storing-and-recalling-script-variables-in-a-config-file-what-is-the-best-way-4175559621/)

mxmaniac 11-23-2015 12:42 AM

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"
Or if you want to create a new folder every time the script is ran, it could just be along the lines of
Code:

var6= line 6 of conf file; mkdir directory$var6; write var6+1 back to config file.
If this is a good way to handle semi permanent variables, then what is the best way to read/store them?

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

chrism01 11-23-2015 01:37 AM

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
declare -a fc_arr
mapfile -t fc_arr < t.t
#
# get arr content; short method
printf "s\n" "${fc_arr[@]}" ; exit
#
# long
for ((i=0; i<${#fc_arr[@]}; i++))
do
    echo ${fc_arr[$i]}
done

The above enables you to process one var at a time, changing the value and storing back in the array, then print the array out into the orig file (you may want to maintain eg 3 backups so you can backtrack if reqd, at least for testing).

Bash arrays: http://wiki.bash-hackers.org/syntax/arrays

HTH

zhjim 11-23-2015 02:53 AM

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:

#config_file
root="/var/www"
index="index.html"
admins="admin"

#work_horse
. ./config_file
This way you would have the three variables set and usuable in the file work_horse.

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:

config="/path/to/config/file"
function write_config {
echo 'root="/var/www/new"' > $config
echo 'index="index.php" > $config
admins="admin peter" > $config
# Could use <<< EOF > $config
}
Depending on the actual use case you might want to export all the variables from the config file.

jpollard 11-23-2015 03:13 AM

Quote:

Originally Posted by zhjim (Post 5453890)
...
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.


Depending on the actual use case you might want to export all the variables from the config file.

Actually, it would be better to redirect the output to a new file, then after the new file is finished "mv new.config old.config" - that way if the script gets aborted you don't lose data...


All times are GMT -5. The time now is 07:42 PM.