LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-30-2011, 09:28 PM   #16
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682

Quote:
Originally Posted by Dimitriy View Post
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.


thank you,
d
Cat is simply writing to the file. It prints out the contents of the file, or of stdin if there is no file argument. So in this case, it is printing the contents of STDIN, which is coming from the HERE document.
Instead of cat'ing a template file, you are cat'ing the contents of the HERE document, and writing the contents to a file after the variables are evaluated.

It may be that you didn't realize that cat would print the contents of stdin. The << redirects the contents of the rest of the file up to the terminator to stdin, so that is what `cat' reads and prints. The redirection operator writes to a file instead of stdio.

So the stream goes from installscript --> `<<' --> stdio --> cat --> stdout --> file.

Also look at the form of variable evaluation: ${variable:=default}
This allow you to give the variable a default value. If it doesn't have a value, the variable evaluates to the default.

Another advantage to using HERE documents is that you can have more than one in your script. The same script could create and edit a number of config files.

Last edited by jschiwal; 04-30-2011 at 09:34 PM.
 
Old 04-30-2011, 10:04 PM   #17
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
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
 
Old 05-01-2011, 12:07 AM   #18
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
After \i or \a, you need to press return first. The \ character causes sed to treat the newline literally and not end the sed command.

Also, if you want variables evaluated inside a sed command, put them in doublequotes.

You can use the -e option to add commands to sed instead of running sed multiple times.

sed -e 'command1' -e 'command2' \
-e 'command3' ...' file

Another way is to separate commands with the semicolon:
sed 'command1;command2;command3' file
 
Old 05-03-2011, 03:11 AM   #19
Dimitriy
Member
 
Registered: Oct 2005
Distribution: Ubuntu Dapper (6.06)
Posts: 92

Original Poster
Rep: Reputation: 15
Hmm...
Installer.sh
Code:
sed -i '2a\cp /mnt/'$servicesvolume/$servicesdir'/subsonic/usr-lib/* /usr/lib' /mnt/$servicesvolume/$servicesdir/subsonic/subsonic.sh
sed -i '20a\SUBSONIC_DEFAULT_PLAYLIST_FOLDER='/mnt/$servicesvolume/$servicesdir/subsonic/playlists'' /mnt/$servicesvolume/$servicesdir/subsonic/subsonic.sh
sed -i '20a\SUBSONIC_DEFAULT_MUSIC_FOLDER=' /mnt/$servicesvolume/$servicesdir/subsonic/subsonic.sh
sed -i '20a\SUBSONIC_MAX_MEMORY=256' /mnt/$servicesvolume/$servicesdir/subsonic/subsonic.sh
This works properly for me.... without the returns

---------- Post added 05-03-11 at 08:12 AM ----------

Another solution that may work:
installer.sh:
Code:
mydefinedhome=/mnt/blah/subsonic
sed -i s@SUBSONIC_HOME=/var/subsonic@"$mydefinedhome"@ subsonic.sh
~dpc
 
  


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 10:31 PM.

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