archive regenerating shell script

I need to create a shell program which generates special "archives". These archives will also be shell scripts, so that they can be executed with "sh ".

Let's say I have some directory structure like:

mydir +- file.txt +- mysubdir +- foo.txt 

file.txt contains "lorem ipsum" and foo.txt contains "foo bar".

Then executing the archive program:

$ arch.sh mydir > bundle.sh

Should generate a file named "bundle.sh" which may look something like this:

if [ \! -d mydir ] ; then mkdir mydir cd mydir if [ -f file.txt ] ; then touch file.txt echo "lorem ipsum" >> file.txt fi if [ \! -d mysubdir ] ; then mkdir mysubdir cd mysubdir if [ -f foo.txt ] ; then touch foo.txt echo "foo bar" >> foo.txt fi fi fi 

So, basically, if you would execute this bundle script then you would end up with exactly the same directory structure and file contents that you had when you generated the bundle. Now, only need to write a program which iterates over all the files and directories and creates the appropriate output for each. For text files, you could process them with sed to convert every line in the text file to a shell command. For example, if the file looks like this:

Foo Bar Baz Goo 

Then you could generate the following shell commands:

echo "Foo" >> example.txt echo "Bar" >> example.txt echo "Baz" >> example.txt echo "Goo" >> example.txt 

For binary files, you should use "uuencode" or maybe you can also use "base64", e.g. when you have a binary file "file.bin" then executing the following commands output a shell script which recreates the binary file:

base64 file.bin | sed 's/.*/echo\ \0 >> echo "base64 -d file.txt > file.bin" echo "rm file.txt" 

So in this way, I can just email the bundle.sh commands to a friend, and then when he executes bundle.sh on his machine, he will generate exactly the same directory structure as, say, mydir.

I want to know how to do the recursion to traverse the directory and how to get the name of each file and how to bundle all the shell commands that I generated to bundle.sh. Thanks. guys.

5

1 Answer

Using makeself :

$ cd /path/to/dir $ ls main.sh dir1 dir2 dir3 $ makeself . ../bundle.run "Creating a bundle with makeself" ./main.sh 

main.sh is the main script. The bundle will contains the full recursive dirs and your main script.

To run the bundle :

chmod +x bundle.run ./bundle.run 
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like