Home

Embedding files into shell scripts

This presents an easy way to distribute files is in a self-contained archive which can run commands; fortunately, UNIX has had ways of accomplishing this for ages.

What you need

Our example will install a .tar.gz into a prompted-for directory

First let's answer a question about the BASEDIR.

You can also see some shell tricks here to keep track of where this file is located

#!/bin/sh 
dir=`dirname $0`;
if [ x$dir = "x." ]
then
    dir=`pwd`
fi
base=`basename $0`;
printf "Enter the base directory for installation\n"
printf "myApp will be installed in BASE/myApp\n"
printf "> "
read path in

Now we will add the commands to unencode and untar our file to stdout and install it in $path

(cd $path; uudecode -p $dir/$base|tar xzfv -)
exit 0;

And now add the actual binary to the shell script

Note: This is done from the shell, not in the script

uuencode myApp.tar.gz myApp.tar.gz >> myApp_install.sh

Which will yield:

begin 644 myApp.tar.gz
M'XL("%_E\T8``VUY07!P+G1A<@#M7'UP7-5U/V\_I)4LA%/4`+'BK(SL6L9X
MM9)LKV4+)+,.2!3-WQIB.N0
MEC#DHRV9I$8AD%*&=$@'9@A)&`A,)RUT:I>D=5-C]7<_WH<^EPQ#^D?WSCR]
...
end

into the script.

Now we can see a full run of the program

I've also included some other commands to give an idea of location, etc

Matthews-Apple:~ msporleder$ pwd
/Users/msporleder
Matthews-Apple:~ msporleder$ /tmp/myApp_install.sh 
Enter the base directory for installation
myApp will be installed in BASE/myApp
> /var/tmp/
myApp/
myApp/hello
Matthews-Apple:~ msporleder$ /var/tmp/myApp/hello 
Hello, world
Goodbye, world
14, 4

Appendix -- The full script --use View Source to get the actual hello program (works on osx/ppc)

#!/bin/sh
dir=`dirname $0`;
if [ x$dir = "x." ]
then
        dir=`pwd`
fi
base=`basename $0`;
printf "Enter the base directory for installation\n"
printf "myApp will be installed in BASE/myApp\n"
printf "> "
read path in

(cd $path; uudecode -p $dir/$base|tar xzfv -)
exit 0;
begin 644 myApp.tar.gz
M'XL("%_E\T8``VUY07!P+G1A<@#M7'UP7-5U/V\_I)4LA%/4`+'BK(SL6L9X
MM9)LKV4+)+,.2!3-WQIB.N0
MEC#DHRV9I$8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