LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 04-29-2011, 12:26 AM   #1
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Rep: Reputation: 15
Simple Modify of Variables in bash file + Inject one line.


Hi,

Im trying to put together a install script (installer.sh) that installs a couple programs and I am having trouble figuring out a step for modifying variables on a external file. Seems ridiculously simple but I cant seem to get it to change properly using sed or similar.

Here is the relevant snippet of the original file:
subsonic.sh
Code:
#!/bin/sh
SUBSONIC_HOME=/var/subsonic
SUBSONIC_PORT=8084
SUBSONIC_DEFAULT_MUSIC_FOLDER=/var/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/var/music/Podcast
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/var/playlists
In my install script I am prompting the user for the info, assign it to a variable and then want to replace the current values into the script to look something like this:

subsonic.sh (modified by shell script)
Code:
#!/bin/sh
JAVA_HOME=/usr/lib/java  <-- ADDED a NEW LINE
SUBSONIC_HOME=/mnt/disk1/_soft/ss
SUBSONIC_PORT=9000
SUBSONIC_DEFAULT_MUSIC_FOLDER=/mnt/disk1/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/mnt/disk1/_soft/ss/podcasts
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/mnt/disk1/_soft/ss/playlists
What could I put into my intaller.sh script that could make those changes?

EDIT - SOLVED:
Decided on the following solution:

Code:
echo "Modifying subsonic.sh"
sed -i '1a\JAVA_HOME=/usr/lib/java' subsonic.sh
sed -i '2a\cp /mnt/'$servicesvolume/$servicesdir'/subsonic/usr-lib/* /usr/lib' subsonic.sh
sed -i '20a\SUBSONIC_DEFAULT_PLAYLIST_FOLDER='/mnt/$servicesvolume/$servicesdir/subsonic/playlists'' subsonic.sh
sed -i '20a\SUBSONIC_DEFAULT_MUSIC_FOLDER=' subsonic.sh
sed -i '20a\SUBSONIC_MAX_MEMORY=256' subsonic.sh
sed -i '20a\SUBSONIC_PORT='$subsonicport'' subsonic.sh
sed -i '20a\SUBSONIC_HOME='/mnt/$servicesvolume/$servicesdir/subsonic/home'' subsonic.sh
sed -i '20a\#Begin dpc install script variable edits' subsonic.sh
thank you all,
d

Last edited by Dimitriy; 04-30-2011 at 10:05 PM.
 
Old 04-29-2011, 12:52 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
If the script is not that long something like this
Code:
# Get the length of the install.sh
LN=$(wc -l install.sh)
# minus one (thats not correct look it up)
LN=$((LN - 1))
# Get the header of install.sh
head -n 1 install.sh > /tmp/new
# Add the new line
echo "JAVA_HOME=/path/to/jave" >> /tmp/new
# Get the rest
tail -n $LN install.sh >> /tmp/new
# make it executable
chmod u+x /tmp/new
# run it
/tmp/new
 
Old 04-29-2011, 12:53 AM   #3
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
In installer.sh:

Code:
sed -i '2 i\
JAVA_HOME=/usr/lib/java' subsonic.sh
That inserts the JAVA_HOME variable before the second line.

Code:
$ cat installer.sh
#!/bin/bash

sed -i '2 i\
JAVA_HOME=/usr/lib/java' subsonic.sh

$ cat subsonic.sh
#!/bin/sh
SUBSONIC_HOME=/var/subsonic
SUBSONIC_PORT=8084
SUBSONIC_DEFAULT_MUSIC_FOLDER=/var/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/var/music/Podcast
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/var/playlists

$ ./installer.sh
$ cat subsonic.sh
#!/bin/sh
JAVA_HOME=/usr/lib/java
SUBSONIC_HOME=/var/subsonic
SUBSONIC_PORT=8084
SUBSONIC_DEFAULT_MUSIC_FOLDER=/var/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/var/music/Podcast
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/var/playlists

$
This requires GNU sed, which I assume you're using. Modify if you're running non-GNU sed.

Last edited by rocket357; 04-29-2011 at 12:55 AM.
 
Old 04-29-2011, 12:53 AM   #4
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
sed onliners just rule
Can one just not add JAVA_HOME=/path/to/java to the enviroment variables?
Code:
export JAVA_HOME="/path/to/java"

Last edited by zhjim; 04-29-2011 at 12:55 AM. Reason: another thought
 
Old 04-29-2011, 12:57 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can use the i and a commands to insert or add lines:
Code:
sed -i '1a\
JAVA_HOME=/usr/lib/java' config
Another thing you can do is source the template. Accept changes, and re-write the config file.
Sometimes a HERE document is used for writing config files:
Code:
...
cat >config <<- EOF
#!/bin/sh
   JAVA_HOME=$JAVA_HOME
   SUBSONIC_HOME=$SUBSONIC_HOME
   SUBSONIC_PORT=$SUBSONIC_PORT
   SUBSONIC_DEFAULT_MUSIC_FOLDER=$SUBSONIC_DEFAULT_MUSIC_FOLDER
   ...
EOF
The items before the EOF marker will be written out. If you don't put quotes around EOF in the first line, variables will be expanded first.
This allows you to easily create files from templates, and the template is contained in the install script itself, simplifying maintenance.
 
Old 04-29-2011, 12:58 AM   #6
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
That was quick! I will go ahead and give the sed onliner for the java a go.

Any ideas on modifying the variables though?

thank you,
dimitriy
 
Old 04-29-2011, 12:59 AM   #7
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
jschiwal...that Kindle snippet in your sig is awesome. Makes me want to buy a Kindle suddenly.
 
Old 04-29-2011, 01:01 AM   #8
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
You can use the i and a commands to insert or add lines:...
This allows you to easily create files from templates, and the template is contained in the install script itself, simplifying maintenance.
True. But in this case I am not generating the whole file (or any part of it). I want to modify the existing variables from whats currently in the file to a new value (something a user inputs as part of the install script). Does that make sense? Its late and my head is turning with all my scripting attempts....

thanks,
d
 
Old 04-29-2011, 01:04 AM   #9
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Take a closer look at jischwals post. (I did not get it the first time )
Anyways you would have the user set all the variables like: $JAVA_HOME, $SUBSONIC_* and then source the installer like said in jischwals post. So something like

Code:
# loop through all the variables you need
# Get the input from the user
# assing to variables
# loop through all the vars end

# jischwals second code post

# I KNOW THE CONFIG IS GOOD else look at it
# run config file
 
Old 04-29-2011, 01:11 AM   #10
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
You mean something like this? (I typed in the italics text)

Code:
$ cat installer.sh
#!/bin/bash

read -p "JAVA_HOME: " JAVA_HOME
sed -i "2 i\
JAVA_HOME=$JAVA_HOME" subsonic.sh

$ cat subsonic.sh
#!/bin/sh
SUBSONIC_HOME=/var/subsonic
SUBSONIC_PORT=8084
SUBSONIC_DEFAULT_MUSIC_FOLDER=/var/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/var/music/Podcast
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/var/playlists

$ ./installer.sh
JAVA_HOME: /usr/lib/whatever
$ cat subsonic.sh
#!/bin/sh
JAVA_HOME=/usr/lib/whatever
SUBSONIC_HOME=/var/subsonic
SUBSONIC_PORT=8084
SUBSONIC_DEFAULT_MUSIC_FOLDER=/var/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/var/music/Podcast
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/var/playlists

Last edited by rocket357; 04-29-2011 at 01:18 AM.
 
Old 04-29-2011, 05:39 PM   #11
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
jschiwal: I kinda see what you are saying but I am not fully grasping the idea. The sed inserting i/a commands for the JAVA path makes sense.

But the sourcing template part I do not fully understand.

installer.sh (focusing on the variables for the moment)
Code:
...

cat >subsonic.sh <<- EOF
#!/bin/sh
   JAVA_HOME=$JAVA_HOME
   SUBSONIC_HOME=$SUBSONIC_HOME
   SUBSONIC_PORT=$SUBSONIC_PORT
   SUBSONIC_DEFAULT_MUSIC_FOLDER=$SUBSONIC_DEFAULT_MUSIC_FOLDER
EOF
Vairables expanding first makes sense. What I can't seem to understand is what the cat command is doing.

It seems to me the cat command is fetching subsonic.sh and then inserting at the end of the file the variables - but the same variables exist in the beginning of the file already...

I may be wrong but wouldnt it make sense to have installer.sh do something like this?:
Code:
subsonic.sh awk "SUBSONIC_HOME=" replace with "SUBSONIC_HOME=1234"
Apologies if it muddies the waters...

thank you,
d
 
Old 04-29-2011, 06:21 PM   #12
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I feel like this actually calls for a different solution. You should really put these variables into a configuration file, and source that file in the script to get the values.

I don't know what else is in your install script, but if there's not a non-interactive way of calling it, I as a package maintainer will be very unhappy. Why is this using an install script instead of "make install", anyway?

Last edited by tuxdev; 04-29-2011 at 06:23 PM.
 
Old 04-29-2011, 06:25 PM   #13
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Assume that subsonic.sh is constantly changing. Version 1, 2 ,3 etc etc.

Assume the variable questions exist in every version though.

Is there no simple way to tell a install script that I want to change the simple variables in another file?

Quote:
I feel like this actually calls for a different solution. You should really put these variables into a configuration file, and source that file in the script to get the values.
Its not bad idea - push a line in the script after the variable to cat another file? but then that breaks the editing ability of the application. (ie down the line if you change a variable through the web front end it doesn't propagate to the variable file...

"Why is this using an install script instead of "make install", anyway?"
Perhaps install script is wrong word - its a bash script that ,downloads and then extracts certain files to a certain location and then attempts to modify the variables in a file from the extracted archive...

~dpc

Last edited by Dimitriy; 04-29-2011 at 06:27 PM.
 
Old 04-29-2011, 11:42 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am not 100% sure I understand the issue, but looking at post #1 i found something curious.
So you start with:
Code:
#!/bin/sh
SUBSONIC_HOME=/var/subsonic
SUBSONIC_PORT=8084
SUBSONIC_DEFAULT_MUSIC_FOLDER=/var/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/var/music/Podcast
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/var/playlists
And your change is:
Code:
#!/bin/sh
JAVA_HOME=/usr/lib/java  <-- ADDED a NEW LINE
SUBSONIC_HOME=/mnt/disk1/_soft/ss
SUBSONIC_PORT=9000
SUBSONIC_DEFAULT_MUSIC_FOLDER=/mnt/disk1/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=/mnt/disk1/_soft/ss/podcasts
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=/mnt/disk1/_soft/ss/playlists
So I will ignore the JAVA_HOME change as others have already presented ideas on how to do this, but except for some small changes the other lines are all fairly similar
and also have crossover values.

Maybe consider the following ideas:
Code:
#!/bin/sh
SUBSONIC_HOME=/var/subsonic
SUBSONIC_PORT=8084
SUBSONIC_DEFAULT_MUSIC_FOLDER=${SUBSONIC_HOME}/music
SUBSONIC_DEFAULT_PODCAST_FOLDER=${SUBSONIC_DEFAULT_MUSIC_FOLDER}/Podcast
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=${SUBSONIC_DEFAULT_MUSIC_FOLDER}/playlists
So not exactly the same as yours but now you would only have to change the first 2 entries in other files.

Second idea:
Code:
#!/bin/sh

[[ -f ~/user.config ]] && . ~/user.config

SUBSONIC_HOME=${SUBSONIC_HOME:-/var/subsonic}
SUBSONIC_PORT=${SUBSONIC_PORT:-8084}
SUBSONIC_DEFAULT_MUSIC_FOLDER=${SUBSONIC_DEFAULT_MUSIC_FOLDER:-${SUBSONIC_HOME}/music}
SUBSONIC_DEFAULT_PODCAST_FOLDER=${SUBSONIC_DEFAULT_PODCAST_FOLDER:-${SUBSONIC_DEFAULT_MUSIC_FOLDER}/Podcast}
SUBSONIC_DEFAULT_PLAYLIST_FOLDER=${SUBSONIC_DEFAULT_PLAYLIST_FOLDER:-${SUBSONIC_DEFAULT_MUSIC_FOLDER}/playlists}
Now this idea allows you to simply have a config in the users directory with the appropriate changes:
Code:
# user.config
SUBSONIC_HOME=/mnt/disk1/_soft/ss
SUBSONIC_PORT=9000
Should hopefully give you some ideas at least
 
Old 04-30-2011, 06:04 PM   #15
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Thanks grail - decided that I am simply avoid scripting this out at this time.

Its a simple idea but solution seems tough. The crux of scenario is below:
> installer.sh - script i create to install subsonic
> subsonic.sh - not to be recreated - magically walk in and edit the variables
> User should never need to manually need to edit subsonic.sh
> subsonic.sh is not generated - its part of a downloaded archive. As such it is subject to be changed and how those changes will be, installer.sh will have no idea on how that change will be. In essence I am gambling the variables stay the same though.
> Installer.sh would ideally be able to modify the subsonic.sh - not recreate it.
> Easiest solution is to have the user edit subsonic.sh after installer.sh is completed.

The unfinished code looks something like this:
installer.sh
Code:
echo "Downloading SubSonic"
mkdir /mnt/disk1/subsonic
cd /mnt/disk1/subsonic
wget -nv http://downloads.sourceforge.net/project/subsonic/subsonic/4.4/subsonic-4.4-standalone.tar.gz
echo "Installing SubSonic"
tar xzf subsonic*
rm subsonic*.tar.gz
#Begin Editing the extracted subsonic.sh file
sed -i '1a\
JAVA_HOME=/usr/lib/java' /mnt/disk1/subsonic/subsonic.sh
#echo "-- Port on which SubSonic will run on (must be higher than 1024), e.g. 8084 --"
#read subsonicport
#echo "-- SubSonic Home dir e.g. /mnt/disk1/subsonic --"
#read subsonichome
echo "EDIT /mnt/disk1/subsonic/subsonic.sh manually to modify the needed variables"
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to modify a line of a file using shell script? shayori Linux - Newbie 9 04-16-2010 02:44 AM
[SOLVED] BASH: Modify file worm5252 Programming 4 03-18-2010 08:26 AM
How to read in line-by-line from a file and store them in several variables ncjlee Programming 1 09-28-2008 06:20 PM
Inject/modify files in iso image on the fly brianmcgee Linux - Software 10 08-01-2008 02:08 AM
Simple Question - how to modify file attributes? xanas3712 Linux - Newbie 12 04-30-2004 07:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration