views:

22

answers:

2

I often have the following scenario: in order to reproduce a bug for reporting, I create a small sample project, sometimes a maven multi module project. So there may be a hierarchy of directories and it will usually contain a few small text files. Standard procedure would of course be to create a zip file and send that. But on some mailing lists attachments are not allowed, and so I am looking for a way to automatically create an installation script, that I can post to such mailing lists.

Basically I would be happy with a Unix flavor only that creates mkdir statements to create directories and >> statements to write the file contents. (Actually, apart from the relative path delimiters, the windows and unix versions can probably be identical)

Does such a tool exist somewhere? If not, I'll probably write one in java, but I'm happy to accept solutions in all kinds of languages.

(The tool could run under windows or unix, but the target platform for the generated scripts should be either unix or configurable)

+1  A: 

I think you're looking for shar, which creates a shell archive (shell script that when run produces a given directory hierarchy). It is available on most systems; you can use GNU sharutils if you don't already have it.

Normal usage for packing up a directory tree would be something like:

shar `find somedirectory -print` > archive.sh

If you're using GNU sharutils, and want to create "vanilla" archives which use only the most portable of shell builtins, mkdir, and sed, then you should invoke it as shar -V. You can remove some more extra baggage from the scripts by using -xQ; -x to remove checks for existing files, and -Q to remove verbose output from the archive.

shar -VxQ `find somedir -print` > archive.sh

If you really want something even simpler, here's a dirt-simple version of shar as a shell script. It takes filenames on standard input instead of arguments for simplicity and to be a little more robust.

#!/bin/sh

while read filename 
do 
    if test -d $filename
    then
        echo "mkdir -p '$filename'"
    else
        echo "sed 's/^X//' <<EOF > '$filename'"
        sed 's/^/X/' < "$filename"
        echo 'EOF'
    fi
done

Invoke as:

find somedir -print | simpleshar > archive.sh

You still need to invoke sed, as you need some way of ensuring that no lines in the here document begin with the delimiter, which would close the document and cause later lines to be interpreted as part of the script. I can't think of any really good way to solve the quoting problem using only shell builtins, so you will have to rely on sed (which is standard on any Unix-like system, and has been practically forever).

Brian Campbell
While this is nice, it makes assumptions about what must be installed on the target system. I would like the script to be able to deal with what's available on every standard system of a given platform (with platform being as abstract as: either a) anything with an x [linux, bsd, macos etc] or b) any windows since win2k)
seanizer
If you're using GNU sharutils, you can use `shar -V` to produce "vanilla" shar archives, which depend on only basic shell builtins, `mkdir`, and `sed`. The shar I tested on Mac OS X (the FreeBSD shar) only ever creates vanilla shar files.
Brian Campbell
sounds great, I'll have a look at it tomorrow
seanizer
I have solved my original problem differently, so I have not come to investigate this. However, I'll keep it in mind and accept it as the correct answer. Thanks
seanizer
A: 

if your problem are non-text-file-hating filters:

in times long forgotten, we used uuencode to get past 8-bit eating relays - is that a way to get past attachment eating mail boxes these days ? So why not zip and uuencode ? (or base64, which is its younger cousin)

blabla999
Well yes, that's a solution to the underlying problem, thanks. But it's not an answer to my question :-)
seanizer