binary file within a shell script

ari edelkind edelkind-freebsd-hackers at episec.com
Thu May 8 18:08:23 UTC 2008


mathieu.prevot at gmail.com wrote:

> I would like to use one exec file from a shellscript but I would like
> it to be incorporated in the same file, like Nvidia do for its FreeBSD
> drivers. How can I do this in a convenient way ?

I haven't looked at nvidia's driver packaging, but you can embed
binaries into shell scripts using uuencode or base64.

Example:
------------------------
% cat >test.sh
#!/bin/ksh

echo "*** generating ls..."
file=`mktemp /tmp/ls.XXXXXX`
[[ $? -eq 0 ]] || exit 1

uudecode -o $lsfile <<'__EOM__'
^D
% uuencode ls </bin/ls >>test.sh
% cat >>test.sh
__EOM__

chmod +x $lsfile
echo "*** running $lsfile ..."
$lsfile
echo "*** cleaning up"
rm -f $lsfile
^D
------------------------

Note that i used single quotes in the here-document initialization, so
there won't be any shell expansion of the uuencoded data.

A few commonly-installed programs that may suit your needs:
  - uuencode / uudecode
  - base64
  - b64
  - openssl base64


If relying on one of the above is infeasible:

You can't portably use inline binary data in a shell script without
preprocessing it (as with one of the above programs), since most shells
can't handle binary zeros; shar(1) fails in this case.  You could,
theoretically, write a small, clever wrapper function to account for the
issue.  You'd also have to ensure that regexp("^__EOM__$") (in the above
example) doesn't exist within the file contents, and note that
excessively long lines may not be handled efficiently by the shell.
You'll need to account for files that do or don't end in a newline,
possibly by always appending an extra newline, then stripping it upon
extraction.  Lastly, you'll need to consider whether you must account
for CR/LF conversion in file transfers or while editing your script.

You probably won't want to deal with all of this, and would be better
off leaving it for out-of-band extraction, such as with dan's gzexe
suggestion.  You'll still need to determine whether CR/LF conversion may
be an issue for you.

ari




More information about the freebsd-hackers mailing list