Patch-ID: bash40-021
[bash.git] / examples / scripts.v2 / uuenc
blob480aa48e3e35ce7bc206c50de2472172583db629
1 #! /bin/bash
3 # original from:
4 # @(#) uuenc.ksh 1.0 93/09/18
5 # 93/09/18 john h. dubois iii (john@armory.com)
7 # conversion to bash v2 syntax by Chet Ramey
9 istrue()
11 test 0 -ne "$1"
14 isfalse()
16 test 0 -eq "$1"
19 phelp()
21 echo "$name: uuencode files.
22 $Usage
23 For each filename given, $name uuencodes the file, using the final
24 component of the file's path as the stored filename in the uuencoded
25 archive and, with a .${SUF} appended, as the name to store the archive in.
26 Example:
27 $name /tmp/foo
28 The file /tmp/foo is uuencoded, with \"foo\" stored as the name to uudecode
29 the file into, and the output is stored in a file in the current directory
30 with the name \"foo.${SUF}\".
31 Options:
32 -f: Normally, if the file the output would be stored in already exists,
33 it is not overwritten and an error message is printed. If -f (force)
34 is given, it is silently overwritten.
35 -h: Print this help."
38 name=${0##*/}
39 Usage="Usage: $name [-hf] <filename> ..."
40 typeset -i force=0
42 SUF=uu
44 while getopts :hf opt; do
45 case $opt in
46 h) phelp; exit 0;;
47 f) force=1;;
48 +?) echo "$name: options should not be preceded by a '+'." 1>&2 ; exit 2;;
49 ?) echo "$name: $OPTARG: bad option. Use -h for help." 1>&2 ; exit 2;;
50 esac
51 done
53 # remove args that were options
54 shift $((OPTIND - 1))
56 if [ $# -lt 1 ]; then
57 echo "$Usage\nUse -h for help." 1>&2
58 exit
61 for file; do
62 tail=${file##*/}
63 out="$tail.${SUF}"
64 if isfalse $force && [ -a "$out" ]; then
65 echo "$name: $out: file exists. Use -f to overwrite." 1>&2
66 else
67 uuencode $file $tail > $out
69 done