LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   building a bash script from an install script (http://www.linuxquestions.org/questions/programming-9/building-a-bash-script-from-an-install-script-445477/)

paranoid times 05-16-2006 04:44 PM

building a bash script from an install script
 
I have a bash script that I am going to put on many different computers. Some variables can change from computer to computer such as where my script is supposed to copy files too. What I am wondering is can I make an install script for my bash script, and have the install script ask me "where do you want this script to copy files too" then the install script fills in the blanks in my bash script? I'm not sure even were to look for this one, so any help would be greatly appriciated.

Thank you,
Michael.

Andrew Benton 05-16-2006 05:44 PM

Something like...
Code:

#!/bin/bash

printf "Where should I put it?"
read place
cp -R files $place

Or you could use that script to call the other script you mentioned and pass it $place as an argument
Code:

#!/bin/bash

printf "Where should I put it?"
read place
/path/to/script $place


Hko 05-16-2006 06:01 PM

Let's say have a simple script like this, called "skript.sh":
Code:

#!/bin/bash

# Below __COPYFROM__ and __COPYTO__ are the placeholders
# which will be replaced by the install script with the
# actual paths entered by the installing user.
#
COPYFROM=__COPYFROM__
COPYTO=__COPYTO__

# Remove "echo" from line below to really do copying.
#
echo cp -v $COPYFROM/* $COPYTO/*

Then here's a install script (say, called "install.sh"):
Code:

#!/bin/bash

# This install script assumes the script-to-be-installed
# is in the current working directory.

SCRIPT="skript.sh"
INSTALLDIR="/usr/local/bin/"

# Ask the installing user for the paths
echo "Installing \"$SCRIPT\"."
read -ep "Please enter path to copy from: " COPYFROM
read -ep "Please enter path to copy to: " COPYTO

sed -e 's:__COPYFROM__:'$COPYFROM':' \
    -e 's:__COPYTO__:'$COPYTO':' \
    $SCRIPT >$INSTALLDIR/$SCRIPT

echo "Done"


paranoid times 05-16-2006 07:29 PM

Andrew Benton, sorry I didn't describe things very well, I wanted something more along the lines of what Hko put up. However I still don't quite understand what is happening with the script, it makes another script that permantly put my variables into place so that I wouldn't have to enter them each time the script was ran. But I don't see how it did that.

Code:

read -ep "Please enter path to copy from: " COPYFROM
read -ep "Please enter path to copy to: " COPYTO

the install script collects the locations that I want there.

Code:

sed -e 's:__COPYFROM__:'$COPYFROM':' \
    -e 's:__COPYTO__:'$COPYTO':' \
    $SCRIPT >$INSTALLDIR/$SCRIPT

this part confuses me a little. the sed -e is saying wherever we find "__COPYFROM__" replace it with the value of "$COPYFROM", and then again with "COPYTO", then it takes "skript.sh" and makes those changes, but only in ram and doesn't over write "./skript.sh" but instead writes it with changes to "$INSTALLDIR/$SCRIPT". is that right? I think the part that is confusing me is the redirect, if that wasn't there would the sed command just take ./skript.sh make the changes and overwrite it? and the reason that it doesn't overwrite ./skript.sh is because we redirect it?

In any case thank you both very much for your help.

Hko 05-17-2006 12:48 PM

Quote:

Originally Posted by paranoid times
Code:

sed -e 's:__COPYFROM__:'$COPYFROM':' \
    -e 's:__COPYTO__:'$COPYTO':' \
    $SCRIPT >$INSTALLDIR/$SCRIPT

this part confuses me a little. the sed -e is saying wherever we find "__COPYFROM__" replace it with the value of "$COPYFROM", and then again with "COPYTO", then it takes "skript.sh" and makes those changes, but only in ram and doesn't over write "./skript.sh" but instead writes it with changes to "$INSTALLDIR/$SCRIPT". is that right?

That's right.
Note: You should not specify INSTALLDIR to be the same as the current working directory, as this will overwrite the sed is still trying to read. This will cause bad results.

Quote:

Originally Posted by paranoid times
I think the part that is confusing me is the redirect, if that wasn't there would the sed command just take ./skript.sh make the changes and overwrite it? and the reason that it doesn't overwrite ./skript.sh is because we redirect it?

If the redirect wasn't there, sed would write the contents to stdout (standard output), which, by default, is your terminal screen. (Just try it!)

With the redirect sed changes the contents of the file "skript.sh", and at the same time install it in the install-dir. You may want to add a line at the end:
Code:

chmod +x $INSTALLDIR/$SCRIPT
..to make sure it can be executed.

paranoid times 05-17-2006 12:59 PM

Awesome I think I get it now. The script is working great. Thanks again.

/bin/bash 07-29-2006 03:24 AM

Quote:

That's right.
Note: You should not specify INSTALLDIR to be the same as the current working directory, as this will overwrite the sed is still trying to read. This will cause bad results.
Or you could use -i switch with sed, which causes sed to edit the file "in-place."
Code:

#!/bin/bash

# This install script assumes the script-to-be-installed
# is in the current working directory.

SCRIPT="/path/to/skript.sh"
#INSTALLDIR="/usr/local/bin/" Don't need this anymore

# Ask the installing user for the paths
echo "Installing \"$SCRIPT\"."
read -ep "Please enter path to copy from: " COPYFROM
read -ep "Please enter path to copy to: " COPYTO

sed -i -e 's:__COPYFROM__:'$COPYFROM':' \
    -e 's:__COPYTO__:'$COPYTO':' $SCRIPT

echo "Done"



All times are GMT -5. The time now is 07:55 AM.