BASH script – reading and writing variables to a separate file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
playback_file_1=/home/user/sounds/foo.mp3
playback_file_2=/home/user/sounds/blah.mp3
…
etc.
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.