summaryrefslogtreecommitdiff
path: root/examples/scripts.v2/uuenc
blob: 480aa48e3e35ce7bc206c50de2472172583db629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /bin/bash
#
# original from:
# @(#) uuenc.ksh 1.0 93/09/18
# 93/09/18 john h. dubois iii (john@armory.com)
#
# conversion to bash v2 syntax by Chet Ramey

istrue()
{
	test 0 -ne "$1"
}

isfalse()
{
	test 0 -eq "$1"
}

phelp()
{
echo "$name: uuencode files.
$Usage
For each filename given, $name uuencodes the file, using the final
component of the file's path as the stored filename in the uuencoded
archive and, with a .${SUF} appended, as the name to store the archive in.
Example: 
$name /tmp/foo
The file /tmp/foo is uuencoded, with \"foo\" stored as the name to uudecode
the file into, and the output is stored in a file in the current directory
with the name \"foo.${SUF}\".
Options:
-f: Normally, if the file the output would be stored in already exists,
    it is not overwritten and an error message is printed.  If -f (force)
    is given, it is silently overwritten.
-h: Print this help."
}

name=${0##*/}
Usage="Usage: $name [-hf] <filename> ..."
typeset -i force=0

SUF=uu

while getopts :hf opt; do
    case $opt in
    h)	phelp; exit 0;;
    f)	force=1;;
    +?)	echo "$name: options should not be preceded by a '+'." 1>&2 ; exit 2;;
    ?)	echo "$name: $OPTARG: bad option.  Use -h for help." 1>&2 ; exit 2;;
    esac
done
 
# remove args that were options
shift $((OPTIND - 1))

if [ $# -lt 1 ]; then
    echo "$Usage\nUse -h for help." 1>&2
    exit
fi

for file; do
    tail=${file##*/}
    out="$tail.${SUF}"
    if isfalse $force && [ -a "$out" ]; then
	echo "$name: $out: file exists.  Use -f to overwrite." 1>&2
    else
	uuencode $file $tail > $out
    fi
done