[SOLVED] Simple Modify of Variables in bash file + Inject one line.
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.
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
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
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
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:
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.
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....
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
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"
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?
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...
#!/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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.