Bash script with an embedded file

In the last months I’m getting used to do some of the tasks at my job using shell scripting, specially tasks that are done repeatedly.

Sometimes I’m lazy to write the script for a simple task that, manually, would take only a few minutes. But if you think about the time that you can save when you have to repeat that task over and over then it’s worthy to write a script.

In this case I wanted a script that generates a defined structure of folders (a Subversion local repository), but it was not as easy as using mkdir command. I had a folder structure that I wanted to replicate but I wanted to distribute only the script in my project. The solution for that was using the uuencode and uudecode commands.

Imagine that you have a folder that contains all your structure called “workspace”, you could create a tar.gz file with that and then, using uuencode, you could embed that file into your shell script:

> tar -zcvf workspace.tar.gz workspace/
> uuencode workspace.tar.gz workspace.tar.gz > generate_workspace.sh

 

That would generate a file with the following content (in this case, workspace is a folder with a text file inside):

begin 644 workspace.tar.gz
M'XL(`*42KDP``^W170K",!!&T3Z[BJR@.C&I&W$#P09:_*6)U.4;*RJ(%1]:
M1+R'P`S,0`:^=M^LP\&M_#0;S2Q96'NM1='5Y%:[7L38N1'1.LU%2UI3=KR3
M'HXAND:IK'2[VF_Z]]K*OQO_JO:>?^-=N?5Y/,6A_[@$7!C3G[]^SM^(-9F:
K#7W(*W^>_[*J@TK/J>A#S"??O@<```````````````#`Y\YXW]L2`"@`````
`
end

Then, you could edit the same file to add the code necessary to extract the file, uncompress it and move it somewhere:


#!/bin/sh
# validate command line arguments
if [ "$1" = "" ]
then
echo "$0: Generates an empty workspace"
echo "Usage: $0 [Workspace path]"
exit
fi
uudecode $0
tar -zxvf workspace.tar.gz > /dev/null
rm workspace.tar.gz
mv workspace $1
echo "Generated empty Workspace in $1"
exit 0
begin 644 workspace.tar.gz
M'XL(`*42KDP``^W170K",!!&T3Z[BJR@.C&I&W$#P09:_*6)U.4;*RJ(%1]:
M1+R'P`S,0`:^=M^LP\&M_#0;S2Q96'NM1='5Y%:[7L38N1'1.LU%2UI3=KR3
M'HXAND:IK'2[VF_Z]]K*OQO_JO:>?^-=N?5Y/,6A_[@$7!C3G[]^SM^(-9F:
K#7W(*W^>_[*J@TK/J>A#S"??O@<```````````````#`Y\YXW]L2`"@`````
`
end

Executing the script to extract the folder structure somewhere:


> chmod +x generate_workspace.sh
> ./generate_workspace.sh /tmp/myWorkspace

And that’s all. I hope that this helps somebody.

Advertisements
This entry was posted in Scripting and tagged . Bookmark the permalink.

2 Responses to Bash script with an embedded file

  1. xereeto says:

    It helped me embed images, thank you!

  2. denian says:

    I’m glad it helped!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s