-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.sh
executable file
·118 lines (95 loc) · 2.38 KB
/
template.sh
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#! /usr/bin/env bash
# <blah blah blah>
# -E allows -e to work with 'trap ... ERR'
set -ueE -o pipefail
# have case and [[ do case-insensitive matches
shopt -s nocasematch
prog="$(basename "$0")"
# SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# source ${SCRIPTDIR}/foo.subs
function usage {
[ "${*-}" ] && echo "$prog: $*" 1>&2
cat 1>&2 <<EOF
Usage: $prog [--switch1] [--switch2 blah]
blah blah blah fix me blah blah blah
EOF
exit 1
}
function errordie {
[ "${*-}" ] && echo "$prog: $*" 1>&2
exit 1
}
if ! TEMP=$(getopt --shell bash -o f: -l debug,file: -n "$prog" -- "$@"); then
exit 1
fi
if ! type -p foo > /dev/null; then
errordie "Cannot find foo, please install it before running $prog"
fi
eval set -- "$TEMP"
debug=
function d {
if [ "$debug" ]; then
echo "would: $*" 1>&2
else
echo "+ $*" 1>&2
"$@"
fi
}
file=
while [ $# -gt 0 ]; do
case $1 in
--debug) debug=$1 ;;
-f|--file)
[ $# -ge 2 ] || usage "$1: missing companion argument"
shift
file="$1"
;;
--) ;;
*) usage extra args: "$@"
##or, choose this if extra arg processing down below
#break
;;
esac
shift
done
[ -f "$file" ] || errordie "$file" does not exist
[ $# -eq 2 ] && echo "do this for the 2 arg case"
lockfile="/tmp/${prog}.lock"
# ensure that only one copy of this script is running at any given time
lockfile -r 0 "$lockfile" || errordie program is already running
tempfile="/tmp/${prog}temp1$$"
tempfile2="/tmp/${prog}temp2$$"
rm -f "$tempfile"
rm -f "$tempfile2"
# shellcheck disable=SC2317
function exit_cleanup {
rm -f "$tempfile"
rm -f "$tempfile2"
rm -f "$lockfile"
}
# shellcheck disable=SC2317
function err_report {
echo "Error on line $(caller)" 1>&2
}
trap err_report ERR
trap exit_cleanup EXIT
output="script.out"
# Send all output to $output AND stdout
exec &> >(tee -a "$output")
# Send all output to $output
exec &> "$output"
# main body is in a list so the script can be changed while in use
{
find ... > "$tempfile"
while IFS=$'\n' read -r line; do
# this stuff happens in the same shell as the main script
echo "$line"
done <<< "$(cat "$tempfile")"
find .. .. |
while IFS='$\n' read -r line; do
# this stuff happens in a subshell and can't modify variables above
echo "$line"
done
...
exit 0
}