LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 09-20-2005, 05:39 AM   #1
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Distribution: Frustrated Ubuntu 6.10 Edgy User
Posts: 257

Rep: Reputation: 30
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!
 
Old 09-20-2005, 05:50 AM   #2
saneax
Member
 
Registered: Aug 2004
Distribution: Gentoo, Suse, Fedora, Debian
Posts: 86

Rep: Reputation: 15
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
 
Old 09-20-2005, 05:52 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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.
Code:
 
.  $SAVEFILE
 
Old 09-20-2005, 05:59 AM   #4
saneax
Member
 
Registered: Aug 2004
Distribution: Gentoo, Suse, Fedora, Debian
Posts: 86

Rep: Reputation: 15
Do this

echo playback_file_1=/home/user/sounds/foo.mp3 >> $SAVEFILE
 
Old 09-20-2005, 06:19 AM   #5
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Distribution: Frustrated Ubuntu 6.10 Edgy User
Posts: 257

Original Poster
Rep: Reputation: 30
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 06:21 AM.
 
Old 09-20-2005, 06:55 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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
 
Old 09-20-2005, 07:21 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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'
 
Old 09-20-2005, 07:22 AM   #8
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Distribution: Frustrated Ubuntu 6.10 Edgy User
Posts: 257

Original Poster
Rep: Reputation: 30
That's great!

I didn't understand the
Quote:
. 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?
 
Old 09-20-2005, 07:34 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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!
 
Old 09-20-2005, 07:41 AM   #10
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Distribution: Frustrated Ubuntu 6.10 Edgy User
Posts: 257

Original Poster
Rep: Reputation: 30
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!
 
Old 09-20-2005, 07:45 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
don't let that stop you:

http://www.cygwin.com/

 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM
Writing to a file - Bash script Skute Programming 2 03-15-2004 04:41 AM


All times are GMT -5. The time now is 10:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration