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.
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.
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"
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.
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:
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.