How To Create A Bash Install Script/.bin installer

Have you ever wanted to create an installer program on a Linux system, but didn’t want all the hassle of an actual install builder? I have. I recently had need of a simple, no frills installation that could:
  1. ask the user a few questions
  2. extract some binary files and
  3. do some stuff with those binary files.
So I’m going to show you how you can create such an installer with very little hassle. I can’t actually claim credit for this method though; I actually got the idea from Sun’s JDK installer for the Linux platform. You download a “.bin” file, change the file mode so that it is executable and then run it. It displays the end user license agreement, gets some feedback and then goes about installing Java for you. Well, if you open that .bin file up in a text editor (say vi) you’ll see that it’s nothing more than a shell script with a binary chunked onto the end. Thus my plans for world domination were born…

The Script

The first part of this process is to create your install script. At the beginning of my script I put any environment variables and setup that I need for the script to run propertly.
For this exercise I chose a zip file as my binary distribution media. The ZIP_FILENAME variable holds the name of the file after extracting it from the binary installer. SCRIPT_LINES holds the number of lines in this script file. Actually it’s the number of lines plus one (more on this later). SUM1 and SUM2 are the two numbers that I got from running /usr/bin/sum on my zip file. We’ll use these numbers to verify the file after extracting it from the archive.

# ------------------------------------------------------------
# Setup Environment
# ------------------------------------------------------------
PATH=/usr/bin:/bin
umask 022
PDIR=${0%`basename $0`}
ZIP_FILENAME=Unpacked.zip
 
# Number of lines in this script file (plus 1)
SCRIPT_LINES=64
 
# Run /bin/sum on your binary and put the two values here
SUM1=07673
SUM2=2
 
The next line uses the trap statement to execute some instructions in case the file exits. This just lets the script clean up after itself in the event that the user exits the file prematurely (ie using CTRL-C to close).
trap 'rm -f ${PDIR}/${ZIP_FILENAME}; exit 1' HUP INT QUIT TERM
Now for the unpacking (or extracting) of our binary distribution media (a zip file in this case). Basically we’re using the tail command to do the job. The -n option indicates that our number argument is the number of lines that we want. Of course you see we’re using the $SCRIPT_LINES variable that we defined at the start of the script. We prepend the plus (+) operator in front of that number which tells the tail command that we want the last lines of the file starting at the number indicated. So we’ll get all of the file starting at the line just past our script, which will be the start of our zip file.

echo "Unpacking binary files..."
tail -n +$SCRIPT_LINES "$0" > ${PDIR}/${ZIP_FILENAME}
 
This little bit performs a checksum on the unpacked zip file and checks that against the values we set when building the installer.

SUM=`sum ${PDIR}/${ZIP_FILENAME}` 
ASUM1=`echo "${SUM}" | awk '{print $1}'`
ASUM2=`echo "${SUM}" | awk '{print $2}'`
if [ ${ASUM1} -ne ${SUM1} ] || [ ${ASUM2} -ne ${SUM2} ]; then
  echo "The download file appears to be corrupted. Please download"
  echo "the file again and re-try the installation."
  exit 1
fi
 
The last little bit to this script does not really do that much. We’ve unpacked and verified our zip file so now we just unzip it. To finish up the script removes the zip file (we’ve already extracted it’s contents) and exits. Of course at this stage you could do any number of things that you wanted. You could compile some code if you wanted, prompt the user for feedback in order to build a configuration… whatever you need to do (that can be scripted).

unzip ${PDIR}/${ZIP_FILENAME} 
rm -f ${PDIR}/${ZIP_FILENAME}
exit 0

Building The Installer

Ok, so we have our install script ready to go and we have a zip file (or binary distribution media). Now what? Building the final installer is actually a trivial matter from here. All we need to do is concatenate the script file (first) and the zip file into our final installer. We could also change the file mode so that it is executable.

cat install_script Text_Document.zip > install_script.bin
chmod 755 install_script.bin
 
Or if you want to get extremely fancy you can create a makefile! With this example a makefile is like hanging a photo on the wall with a jackhammer, but for more complex projects it may actually be warranted.

SCRIPT_FILE=install_script
BIN_FILE=Text_Document.zip
 
all: install_script.bin
 
install_script.bin:
 cat ${SCRIPT_FILE} ${BIN_FILE} > install_script.bin
 chmod 755 install_script.bin
 
clean:
 rm -f install_script.bin
 
With this in a file called Makefile all you have to do is type make and it builds the installer for you.

Thank you so much for stopping by and reading. I sincerely hope that I’ve helped someone out there.

Comments

Popular posts from this blog

configure Netbackup email notification on Unix