1

Got a small project to do. I need to write a self extracting archive script that can be used to concatenate the contents of a directory so that they can be emailed directly from the command line.

I have to write a shell script that will bundle a list of files and/or directories and output their contents to stdout. If there's a directory, then it will recursively traverse each subdirectory. If the file is not ASCII(if it's binary), just perform uuencode on it.

The recommended way to do this is by flagging the beginning of each line of a file with an "X":

X        contents
X        contents
X        contents

This way, sed can be used to extract the contents of a file.

Output should like this:

>ls
ping README.txt testsubdir
>cd testsubdir/
>ls
recursive_test.txt
> ./arch.sh ../testdir/ > bundle
head bundle
.
.

# This SHAR archive contains the following files
# testdir

if test -f testdir
then
echo "That would clobber testdir!"
> mkdir archtest
> cd archtest
> mv ../bundle .
>ls
bundle
> sh bundle
X - ping
-rw-rw-r-- 1 erik joe 54332 Oct 5 10:19 ping
X - README.txt
.............(same pattern)............. README.txt
X - recursive_test.txt
........................................ recursive_test.txt
> ls
bundle testdir

(the working directory is testdir)

We need to first check if it's a file or directory, if it's a directory, walk down it. If it's a file, and if it's a binary, we have to perform uuencode on it. Then use sed to extract the contents.

The problem is that I don't quite understand the exact things that I need to do on each step and the order. I know how to check file types and how to check if a file is a binary or not, but don't know how to do it recursively on each file and walk down subdirectory tree to perform the tests on all the files. And I don't know how to add "X" and how to use the command sed to get the contents flagged by X.

Pls give me some solid examples or hints so that I can have a start and begin to work based on them. Thanks a lot.

Thor
  • 45,082
  • 11
  • 119
  • 130
J Freebird
  • 3,664
  • 7
  • 46
  • 81
  • probably you need the find command to walk through the directory structure recursively. – Lajos Veres Oct 12 '13 at 07:21
  • Good point Lajos. Possibly answered here : http://stackoverflow.com/questions/10523415/bash-script-to-execute-command-on-all-files-in-directory . Look for the 'find' command example – crafter Oct 12 '13 at 09:59
  • Hi, thanks guys. I tried "for file in /directory", but $file didn't actually traverse the whole directory and its subdirectories. Does the for...in... syntax automatically do this? thanks – J Freebird Oct 13 '13 at 05:35

1 Answers1

2

If shar is not enough for you you can make your own solution using tar archive.

First you will make some self extractor script like:

#!/usr/bin/bash

sed '0,/^_ARCHIVE/d' "$0" | tar xvz

exit 0

_ARCHIVE

And than you can add content to it by simple:

tar cz your_directory >> your_sfx.sh

You can add to your self extractor what ever you want, sum checking, additional info, starting of installation script. It is up to you. You can also insert any other filter to modify encoding. For example base64 in sfx script you will use

#!/usr/bin/bash

sed '0,/^_ARCHIVE/d' "$0" | base64 -d | tar xvz

exit 0

_ARCHIVE

and in creation command

tar cz your_directory | base64 >> your_sfx.sh
Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73
  • Hi Hynek, is it for flagging the contents? Could you please explain more about sed and tar? Don't quite understand the code. Sorry I just got to know shell scripting. Thanks a lot for your time. – J Freebird Oct 13 '13 at 05:32
  • `sed` in mine code works as filter to skip self extracting script content. `tar` will bundle and extract directory content in archive. To be honest, I don't understand the flagging part of your question. If you want *write a self extracting archive script that can be used to concatenate the contents of a directory so that they can be emailed directly*, there is solution in mine answer above. Just copy all desired content into one directory and than pack it by `tar` and append to script. – Hynek -Pichi- Vychodil Oct 13 '13 at 19:17
  • Hi Hynek, thanks a lot. but I don't think this is what I'm supposed to do. I guess that's because my question is not very clear. I re-posted the task here: http://stackoverflow.com/questions/19368940/archive-regenerating-shell-script Please take a look if you have time. thanks – J Freebird Oct 14 '13 at 20:44
  • @YongfengZhang: I don't see the problem. If you use version with base64 encoding you will get pure text, so you can email it. – Hynek -Pichi- Vychodil Oct 14 '13 at 22:04