LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 11-23-2015, 01:42 AM   #1
mxmaniac
LQ Newbie
 
Registered: Feb 2014
Posts: 26

Rep: Reputation: Disabled
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
 
Old 11-23-2015, 02:37 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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

Last edited by chrism01; 11-24-2015 at 01:56 AM. Reason: fix typo
 
2 members found this post helpful.
Old 11-23-2015, 03:53 AM   #3
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 11-23-2015, 04:13 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by zhjim View Post
...
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...
 
1 members found this post helpful.
  


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
Storing image file for loading in a cgi-script zak100 Linux - Newbie 1 10-28-2014 02:57 AM
storing the output in two different variables jone kim General 14 02-06-2014 05:14 AM
echo variables into config file. Karas Linux - Newbie 1 11-27-2009 12:15 PM
Storing over 10 variables shoemoodoshaloo Linux - Newbie 5 07-08-2009 10:32 AM
Ubuntu Edgy where is the config file where xset variables can be set as permanent? jim0112 Ubuntu 2 10-01-2007 02:27 PM


All times are GMT -5. The time now is 05:12 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration