LinuxQuestions.org
Register a domain and help support LQ
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 05-16-2006, 05:44 PM   #1
paranoid times
Member
 
Registered: Jan 2005
Distribution: Fedora Core 3, 4
Posts: 135

Rep: Reputation: 16
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.
 
Old 05-16-2006, 06:44 PM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
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
 
Old 05-16-2006, 07:01 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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"
 
Old 05-16-2006, 08:29 PM   #4
paranoid times
Member
 
Registered: Jan 2005
Distribution: Fedora Core 3, 4
Posts: 135

Original Poster
Rep: Reputation: 16
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.
 
Old 05-17-2006, 01:48 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
Old 05-17-2006, 01:59 PM   #6
paranoid times
Member
 
Registered: Jan 2005
Distribution: Fedora Core 3, 4
Posts: 135

Original Poster
Rep: Reputation: 16
Awesome I think I get it now. The script is working great. Thanks again.
 
Old 07-29-2006, 04:24 AM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 06:05 PM
building l;inux from a script in windows bob_man_uk General 1 04-20-2004 06:21 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-06-2003 12:58 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 06:34 AM


All times are GMT -5. The time now is 09:00 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration