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: Code:
# 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! |
If all you want is this (which is the only thing I understood in your jargon..)
Quote:
regards |
Quote:
echo playback_file_1=/home/user/sounds/foo.mp3 > $SAVEFILE Quote:
Code:
|
Do this
echo playback_file_1=/home/user/sounds/foo.mp3 >> $SAVEFILE |
Sorry about the bad grammar and structure, it was written in a rush whilst I had a spare minute in work!
Surely, Quote:
Code:
SAVEFILE=blah 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: |
importing is easy.
if they are of the form: variable_name="value" As i posted, Code:
. file_with_variables probably best to quote the values in case file names have spaces. Code:
echo variable=\"$value\" >> $SAVEFILE |
if you use a naming convention, e.g:
all your variable names start with 'playback' you can do it automagically like so: Code:
#!/bin/bash Code:
$ zxarr |
That's great!
I didn't understand the Quote:
One further question: Would this work if the variables are defined in a function or would they need to be defined beforehand? |
it doesn't matter.
If you 'dot' the file in the bottom level the variables will be visible in the functions below. try it and see! |
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!
|
|
All times are GMT -5. The time now is 10:06 AM. |