-   Programming (
-   -   BASH script – reading and writing variables to a separate file (

morrolan 09-20-2005 05:39 AM

BASH script – reading and writing variables to a separate file
Hi guys,
I’m fairly new to bash, but I’m managing to do what I want so far, except that I am stuck when trying to write and read variables (filenames) from a separate text file.

Basically I have written a simple bash-driven menu system that I’m hoping to use on the gumstix platform to playback certain mp3’s. The variables have to be readable by both bash and perl, as perl is being used to read the input from a custom serial connection consisting of a bank of 12 buttons.

Basically, 5 sounds can be played and 5 recorded, with 2 spare buttons at the moment.

When button 1 is pressed, the mp3 plays once, then exits.

In the menu system I want to be able to set which files will be played when each button is pressed. The menu system asks you for the full path to the file (/home/user/sounds/foo.mp3) and sets the READ result as “playback_file_1” currently.

How can I write “playback_file_1” to a separate line in a text file?

Such as:


# Variable list for wristpad buttons and full filename paths.




Also, in the menu system I need to be able to read what “playback_file_1" is currently set to from the separate text file.

So far I have drawn a complete blank, and have nothing close to workable for this.

Probably an easy request, but many thanks in advance!

saneax 09-20-2005 05:50 AM

If all you want is this (which is the only thing I understood in your jargon..)

How can I write “playback_file_1” to a separate line in a text file?
this is possible... if you are doing an echo to write to a file.. then use echo -n


bigearsbilly 09-20-2005 05:52 AM


How can I write “playback_file_1” to a separate line in a text file?

echo playback_file_1=/home/user/sounds/foo.mp3 > $SAVEFILE


Also, in the menu system I need to be able to read what “playback_file_1" is currently set to from the separate text file.


saneax 09-20-2005 05:59 AM

Do this

echo playback_file_1=/home/user/sounds/foo.mp3 >> $SAVEFILE

morrolan 09-20-2005 06:19 AM

Sorry about the bad grammar and structure, it was written in a rush whilst I had a spare minute in work!



echo playback_file_1=/home/user/sounds/foo.mp3 >> $SAVEFILE
Will append to the end of the file? Now I can see that being useful for "playback_file_2" etc, such as:



echo playback_file_1=/home/user/sounds/foo.mp3 > $SAVEFILE
echo playback_file_2=/home/user/sounds/blah.mp3 >> $SAVEFILE

But my big problem is when the script is terminated or the system turned off, I need to be able to import the variables "playback_file_1" "playback_file_2" back into the script from SAVEFILE, otherwise if I just change "playback_file_1" and leave the rest of the variables as they are, only "playback_file_1" will be written to SAVEFILE, as the rest will be set to the default values in the script, i.e. blank, because no change has been made.

The idea is to read each variable back into the script, change the mp3 file if necessary, and then save ALL of the filenames back into SAVEFILE each time.

Writing a new SAVEFILE each time the script is run is not a problem, as long as the filenames previously written to SAVEFILE are also saved, along with any changes.

Is that any clearer? :scratch:

bigearsbilly 09-20-2005 06:55 AM

importing is easy.
if they are of the form:


As i posted,


.  file_with_variables
that is a dot, full stop.

probably best to quote the values in case file names have spaces.


echo variable=\"$value\"  >> $SAVEFILE

bigearsbilly 09-20-2005 07:21 AM

if you use a naming convention, e.g:
all your variable names start with 'playback'
you can do it automagically like so:




    set | grep ^$PREFIX

    save_variables | while read line; do
      echo $1 $line

playback3="green day"

show_variables ORIGINAL

# save them
# =========
save_variables > saved

# delete them
# ===========
unset playback1 playback2 playback3
show_variables UNSET
echo UNSET playback1=$playback1 playback2=$playback2 playback3=$playback3

# restore them
# ============
. saved
show_variables SAVED


$ zxarr
ORIGINAL playback1=korn
ORIGINAL playback2=offspring
ORIGINAL playback3='green day'
UNSET playback1= playback2= playback3=
SAVED playback1=korn
SAVED playback2=offspring
SAVED playback3='green day'

morrolan 09-20-2005 07:22 AM

That's great!

I didn't understand the

. file_with_variables
, and I didn't realise that this would import the variables back into the script individually.

One further question: Would this work if the variables are defined in a function or would they need to be defined beforehand?

bigearsbilly 09-20-2005 07:34 AM

it doesn't matter.
If you 'dot' the file in the bottom level the variables will be visible in the functions

try it and see!

morrolan 09-20-2005 07:41 AM

Fantastic - I'll give it a try when I get home - stuck to Windows in work, and this script has absolutely nothing to do with work anyway!

bigearsbilly 09-20-2005 07:45 AM

don't let that stop you:


All times are GMT -5. The time now is 01:12 PM.