-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.csh
executable file
·174 lines (157 loc) · 5.09 KB
/
template.csh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/csh -f
#-------------------------------------------------------------------------------
# Script Name: template.csh
# Written by: John W. Woolsey
# Description is located at bottom of script.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
initProg: # initializes program
onintr catch
# program information
set program = `basename $0`
set progRev = `echo '$Revision: 1.2 $' | awk '{print $2}'`
set progDate = `echo '$Date: 2011/11/09 04:54:10 $' | awk '{print $2}' | awk -F/ '{printf "%s/%s/%s",$2,$3,$1}'`
set exitCode = 0
# global settings
set date = `date +%y%m%d%H%M%S`
set tmpDir = "${PWD}/${program}_${USER}_$date"
mkdir $tmpDir
#-------------------------------------------------------------------------------
getArguments: # get command line arguments (position dependent)
if ($#argv == 0) then # expect at least one argument
echo "${program}: ERROR - Missing arguments from command line"
set exitCode = 1
goto printHelp
endif
set options_debug = 0
set outfile = ""
# long option names are not supported on Mac OS X
set argv = (`getopt dhvo: $*`) # get command line arguments
# set argv = (`getopt -a -o dhvo: --long debug,help,version,out: -- $*| sed "s/'//g"`) # get command line arguments
if ($status == 0) then
while ($#argv > 1)
switch ($argv[1])
# case --debug: # debug mode
case -d:
set options_debug = 1
echo "${program}: DEBUG - Running in debug mode"
breaksw
# case --help: # print help message
case -h:
goto printHelp
breaksw
# case --version: # print version message
case -v:
goto printVersion
breaksw
# case --out: # output file specified
case -o:
set outfile = $argv[2]
if ($options_debug) then
echo "${program}: DEBUG - output file = $outfile"
endif
echo -n "" >! $outfile
shift argv
breaksw
case --: # end of options
break
breaksw
default: # unknown option
echo "${program}: ERROR - Unknown option on command line"
set exitCode = 1
goto printHelp
breaksw
endsw
shift argv
end
shift argv
else
echo "${program}: ERROR - Unknown option on command line"
set exitCode = 1
goto printHelp
endif
if ($options_debug) then
echo "${program}: DEBUG - argv = $argv"
endif
if ($#argv == 0) then
echo "${program}: ERROR - Missing arguments from command line"
set exitCode = 1
goto printHelp
endif
#-------------------------------------------------------------------------------
main: # main section
foreach var ($*) # process each file (not contents)
if ($outfile != "") then
echo "${program}: Processing $var" >> $outfile
else
echo "${program}: Processing $var"
endif
end
goto endProg
#-------------------------------------------------------------------------------
catch: # interrupt handler
echo "$program was interrupted or terminated abnormally."
set exitCode = 1
#-------------------------------------------------------------------------------
endProg: # finalizes and ends program
rm -rf $tmpDir
exit $exitCode
#-------------------------------------------------------------------------------
printVersion: # prints program version
echo "$program $progRev $progDate"
goto endProg
#-------------------------------------------------------------------------------
# printHelp: # prints help message
# echo "$program $progRev $progDate"
# echo
# cat << HMSG
# Description:
# C shell script template with command line interface.
#
# Syntax: $program [ -d[ebug] ]
# -h[elp] | -v[ersion] | [ -o[ut] <outfile> ] <infile>
#
# Where:
# <infile> represents the input file(s).
# <outfile> represents the output file.
#
# Options:
# -d[ebug] specify debug mode.
# -h[elp] print program help.
# -o[out] <outfile> specify output filename.
# -v[ersion] print program version.
#
# Example Invocation:
# % $program -o ofile ifile
# HMSG
# goto endProg
#-------------------------------------------------------------------------------
printHelp: # prints help message
echo "$program $progRev $progDate"
echo
cat << HMSG
Description:
C shell script template with command line interface.
Syntax: $program [ -d ] -h | -v | [ -o <outfile> ] <infile>
Where:
<infile> represents the input file(s).
<outfile> represents the output file.
Options:
-d specify debug mode.
-h print program help.
-o <outfile> specify output filename.
-v print program version.
Example Invocation:
% $program -o ofile ifile
HMSG
goto endProg
#-------------------------------------------------------------------------------
# Revision History
#
# $Log: template.csh,v $
# Revision 1.2 2011/11/09 04:54:10 woolsey
# Updated getopts since Darwin Unix does not support long option format.
#
# Revision 1.1 2011/11/09 04:27:42 woolsey
# Initial revision
#