Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
09-20-2005, 06:39 AM
|
#1
|
Member
Registered: Sep 2003
Location: Manchester UK
Posts: 264
Rep:
|
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!
|
|
|
09-20-2005, 06:50 AM
|
#2
|
Member
Registered: Aug 2004
Distribution: Gentoo, Suse, Fedora, Debian
Posts: 86
Rep:
|
If all you want is this (which is the only thing I understood in your jargon..)
Quote:
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
regards
|
|
|
09-20-2005, 06:52 AM
|
#3
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
Quote:
How can I write “playback_file_1” to a separate line in a text file?
|
SAVEFILE=blah
echo playback_file_1=/home/user/sounds/foo.mp3 > $SAVEFILE
Quote:
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.
|
|
|
|
09-20-2005, 06:59 AM
|
#4
|
Member
Registered: Aug 2004
Distribution: Gentoo, Suse, Fedora, Debian
Posts: 86
Rep:
|
Do this
echo playback_file_1=/home/user/sounds/foo.mp3 >> $SAVEFILE
|
|
|
09-20-2005, 07:19 AM
|
#5
|
Member
Registered: Sep 2003
Location: Manchester UK
Posts: 264
Original Poster
Rep:
|
Sorry about the bad grammar and structure, it was written in a rush whilst I had a spare minute in work!
Surely,
Quote:
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:
Code:
SAVEFILE=blah
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? 
Last edited by morrolan; 09-20-2005 at 07:21 AM.
|
|
|
09-20-2005, 07:55 AM
|
#6
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
importing is easy.
if they are of the form:
variable_name="value"
As i posted,
Code:
. file_with_variables
that is a dot, full stop.
probably best to quote the values in case file names have spaces.
Code:
echo variable=\"$value\" >> $SAVEFILE
|
|
|
09-20-2005, 08:21 AM
|
#7
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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
PREFIX=playback
save_variables()
{
set | grep ^$PREFIX
}
show_variables()
{
save_variables | while read line; do
echo $1 $line
done
}
playback1=korn
playback2=offspring
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
Result:
Code:
$ 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'
|
|
|
09-20-2005, 08:22 AM
|
#8
|
Member
Registered: Sep 2003
Location: Manchester UK
Posts: 264
Original Poster
Rep:
|
That's great!
I didn't understand the , 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?
|
|
|
09-20-2005, 08:34 AM
|
#9
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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!
|
|
|
09-20-2005, 08:41 AM
|
#10
|
Member
Registered: Sep 2003
Location: Manchester UK
Posts: 264
Original Poster
Rep:
|
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!
|
|
|
09-20-2005, 08:45 AM
|
#11
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
don't let that stop you:
http://www.cygwin.com/

|
|
|
All times are GMT -5. The time now is 03:44 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|