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.