1 |
xiezhen |
1.1 |
#!/bin/bash
|
2 |
xiezhen |
1.3 |
|
3 |
|
|
|
4 |
|
|
# getopt.sh:
|
5 |
|
|
# functions like getopts but do long-named options parsing
|
6 |
|
|
# and support optional arguments
|
7 |
|
|
#
|
8 |
|
|
# Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain
|
9 |
|
|
# Date created: December 21, 1997
|
10 |
|
|
# Date modified: December 21, 1997
|
11 |
|
|
#
|
12 |
|
|
# IMPORTANT FEATURES
|
13 |
|
|
#
|
14 |
|
|
# 1) Parses both short and long-named options
|
15 |
|
|
# 2) Supports optional arguments
|
16 |
|
|
# 3) Only uses bash builtins, thus no calls to external
|
17 |
|
|
# utilities such as expr or sed is done. Therefore,
|
18 |
|
|
# parsing speed is high enough
|
19 |
|
|
#
|
20 |
|
|
#
|
21 |
|
|
# DESCRIPTION
|
22 |
|
|
#
|
23 |
|
|
# FUNCTION getopt
|
24 |
|
|
# Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS}
|
25 |
|
|
#
|
26 |
|
|
# like getopts, but parse options with both required and optional arguments,
|
27 |
|
|
# Options with optional arguments must have "." instead of ":" after them.
|
28 |
|
|
# Furthemore, a variable name to place option name cannot be specified
|
29 |
|
|
# and is always placed in OPTOPT variable
|
30 |
|
|
#
|
31 |
|
|
# This function is provided for compatibility with getopts()
|
32 |
|
|
# OPTLIST style, and it actually calls getoptex (see bellow)
|
33 |
|
|
#
|
34 |
|
|
# NOTE that a list of parameters is required and must be either "$@",
|
35 |
|
|
# if processing command line arguments, or some alternative parameters.
|
36 |
|
|
#
|
37 |
|
|
# FUNCTION getoptex
|
38 |
|
|
# Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS}
|
39 |
|
|
#
|
40 |
|
|
# like getopts, but parse long-named options.
|
41 |
|
|
#
|
42 |
|
|
# Both getopt and getoptex return 0 if an option has been parsed,
|
43 |
|
|
# and 1 if all options are already parsed or an error occured
|
44 |
|
|
#
|
45 |
|
|
# Both getopt and getoptex set or test the following variables:
|
46 |
|
|
#
|
47 |
|
|
# OPTERR -- tested for whether error messages must be given for invalid options
|
48 |
|
|
#
|
49 |
|
|
# OPTOPT -- set to the name of an option parsed,
|
50 |
|
|
# or to "?" if no more options or error
|
51 |
|
|
# OPTARG -- set to the option argument, if any;
|
52 |
|
|
# unset if ther is no argument;
|
53 |
|
|
# on error, set to the erroneous option name
|
54 |
|
|
#
|
55 |
|
|
# OPTIND -- Initialized to 1.
|
56 |
|
|
# Then set to the number of the next parameter to be parsed
|
57 |
|
|
# when getopt or getoptex will be called next time.
|
58 |
|
|
# When all options are parsed, contains a number of
|
59 |
|
|
# the first non-option argument.
|
60 |
|
|
#
|
61 |
|
|
#
|
62 |
|
|
# OPTOFS -- If a parameter number $OPTIND containg an option parsed
|
63 |
|
|
# does not contain any more options, OPTOFS is unset;
|
64 |
|
|
# otherwise, OPTOFS is set to such a number of "?" signs
|
65 |
|
|
# which is equal to the number of options parsed
|
66 |
|
|
#
|
67 |
|
|
# You might not set variables OPTIND and OPTOFS yourself
|
68 |
|
|
# unless you want to parse a list of parameters more than once.
|
69 |
|
|
# Otherwise, you whould unset OPTIND (or set it to 1)
|
70 |
|
|
# and unset OPTOFS each time you want to parse a new parameters list
|
71 |
|
|
#
|
72 |
|
|
# Option list format is DIFFERENT from one for getopts or getopt. getopts-style
|
73 |
|
|
# option list can be converted to getoptex-style using a function optlistex
|
74 |
|
|
# (see bellow)
|
75 |
|
|
#
|
76 |
|
|
# DESCRIPTION of option list used with getoptex:
|
77 |
|
|
# Option names are separated by whitespace. Options consiting of
|
78 |
|
|
# more than one character are treated as long-named (--option)
|
79 |
|
|
#
|
80 |
|
|
# Special characters can appear at the and of option names specifying
|
81 |
|
|
# whether an argument is required (default is ";"):
|
82 |
|
|
# ";" (default) -- no argument
|
83 |
|
|
# ":" -- required argument
|
84 |
|
|
# "," -- optional argument
|
85 |
|
|
#
|
86 |
|
|
# For example, an option list "a b c help version f: file: separator."
|
87 |
|
|
# defines the following options:
|
88 |
|
|
# -a, -b, -c, --help, --version -- no argument
|
89 |
|
|
# -f, --file -- argument required
|
90 |
|
|
# --separator -- optional argument
|
91 |
|
|
#
|
92 |
|
|
# FUNCTION optlistex
|
93 |
|
|
# Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST`
|
94 |
|
|
#
|
95 |
|
|
# Converts getopts-style option list in a format suitable for use with getoptex
|
96 |
|
|
# Namely, it inserts spaces after each option name.
|
97 |
|
|
#
|
98 |
|
|
#
|
99 |
|
|
# HOW TO USE
|
100 |
|
|
#
|
101 |
|
|
# In order o use in your bash scripts the functions described,
|
102 |
|
|
# include a command ". getopt.sh" to the file containing the script,
|
103 |
|
|
# which will define functions getopt, getoptex, and optlistex
|
104 |
|
|
#
|
105 |
|
|
# EXAMPLES
|
106 |
|
|
#
|
107 |
|
|
# See files 'getopt1' and 'getopt2' that contain sample scripts that use
|
108 |
|
|
# getopt and getoptex functions respectively
|
109 |
|
|
#
|
110 |
|
|
#
|
111 |
|
|
# Please send your comments to grg@philol.msu.ru
|
112 |
|
|
|
113 |
|
|
function getoptex()
|
114 |
|
|
{
|
115 |
|
|
let $# || return 1
|
116 |
|
|
local optlist="${1#;}"
|
117 |
|
|
let OPTIND || OPTIND=1
|
118 |
|
|
[ $OPTIND -lt $# ] || return 1
|
119 |
|
|
shift $OPTIND
|
120 |
|
|
if [ "$1" != "-" -a "$1" != "${1#-}" ]
|
121 |
|
|
then OPTIND=$[OPTIND+1]; if [ "$1" != "--" ]
|
122 |
|
|
then
|
123 |
|
|
local o
|
124 |
|
|
o="-${1#-$OPTOFS}"
|
125 |
|
|
for opt in ${optlist#;}
|
126 |
|
|
do
|
127 |
|
|
OPTOPT="${opt%[;.:]}"
|
128 |
|
|
unset OPTARG
|
129 |
|
|
local opttype="${opt##*[^;:.]}"
|
130 |
|
|
[ -z "$opttype" ] && opttype=";"
|
131 |
|
|
if [ ${#OPTOPT} -gt 1 ]
|
132 |
|
|
then # long-named option
|
133 |
|
|
case $o in
|
134 |
|
|
"--$OPTOPT")
|
135 |
|
|
if [ "$opttype" != ":" ]; then return 0; fi
|
136 |
|
|
OPTARG="$2"
|
137 |
|
|
if [ -z "$OPTARG" ];
|
138 |
|
|
then # error: must have an agrument
|
139 |
|
|
let OPTERR && echo "$0: error: $OPTOPT must have an argument" >&2
|
140 |
|
|
OPTARG="$OPTOPT";
|
141 |
|
|
OPTOPT="?"
|
142 |
|
|
return 1;
|
143 |
|
|
fi
|
144 |
|
|
OPTIND=$[OPTIND+1] # skip option's argument
|
145 |
|
|
return 0
|
146 |
|
|
;;
|
147 |
|
|
"--$OPTOPT="*)
|
148 |
|
|
if [ "$opttype" = ";" ];
|
149 |
|
|
then # error: must not have arguments
|
150 |
|
|
let OPTERR && echo "$0: error: $OPTOPT must not have arguments" >&2
|
151 |
|
|
OPTARG="$OPTOPT"
|
152 |
|
|
OPTOPT="?"
|
153 |
|
|
return 1
|
154 |
|
|
fi
|
155 |
|
|
OPTARG=${o#"--$OPTOPT="}
|
156 |
|
|
return 0
|
157 |
|
|
;;
|
158 |
|
|
esac
|
159 |
|
|
else # short-named option
|
160 |
|
|
case "$o" in
|
161 |
|
|
"-$OPTOPT")
|
162 |
|
|
unset OPTOFS
|
163 |
|
|
[ "$opttype" != ":" ] && return 0
|
164 |
|
|
OPTARG="$2"
|
165 |
|
|
if [ -z "$OPTARG" ]
|
166 |
|
|
then
|
167 |
|
|
echo "$0: error: -$OPTOPT must have an argument" >&2
|
168 |
|
|
OPTARG="$OPTOPT"
|
169 |
|
|
OPTOPT="?"
|
170 |
|
|
return 1
|
171 |
|
|
fi
|
172 |
|
|
OPTIND=$[OPTIND+1] # skip option's argument
|
173 |
|
|
return 0
|
174 |
|
|
;;
|
175 |
|
|
"-$OPTOPT"*)
|
176 |
|
|
if [ $opttype = ";" ]
|
177 |
|
|
then # an option with no argument is in a chain of options
|
178 |
|
|
OPTOFS="$OPTOFS?" # move to the next option in the chain
|
179 |
|
|
OPTIND=$[OPTIND-1] # the chain still has other options
|
180 |
|
|
return 0
|
181 |
|
|
else
|
182 |
|
|
unset OPTOFS
|
183 |
|
|
OPTARG="${o#-$OPTOPT}"
|
184 |
|
|
return 0
|
185 |
|
|
fi
|
186 |
|
|
;;
|
187 |
|
|
esac
|
188 |
|
|
fi
|
189 |
|
|
done
|
190 |
|
|
echo "$0: error: invalid option: $o"
|
191 |
|
|
fi; fi
|
192 |
|
|
OPTOPT="?"
|
193 |
|
|
unset OPTARG
|
194 |
|
|
return 1
|
195 |
|
|
}
|
196 |
|
|
function optlistex
|
197 |
|
|
{
|
198 |
|
|
local l="$1"
|
199 |
|
|
local m # mask
|
200 |
|
|
local r # to store result
|
201 |
|
|
while [ ${#m} -lt $[${#l}-1] ]; do m="$m?"; done # create a "???..." mask
|
202 |
|
|
while [ -n "$l" ]
|
203 |
|
|
do
|
204 |
|
|
r="${r:+"$r "}${l%$m}" # append the first character of $l to $r
|
205 |
|
|
l="${l#?}" # cut the first charecter from $l
|
206 |
|
|
m="${m#?}" # cut one "?" sign from m
|
207 |
|
|
if [ -n "${l%%[^:.;]*}" ]
|
208 |
|
|
then # a special character (";", ".", or ":") was found
|
209 |
|
|
r="$r${l%$m}" # append it to $r
|
210 |
|
|
l="${l#?}" # cut the special character from l
|
211 |
|
|
m="${m#?}" # cut one more "?" sign
|
212 |
|
|
fi
|
213 |
|
|
done
|
214 |
|
|
echo $r
|
215 |
|
|
}
|
216 |
|
|
function getopt()
|
217 |
|
|
{
|
218 |
|
|
local optlist=`optlistex "$1"`
|
219 |
|
|
shift
|
220 |
|
|
getoptex "$optlist" "$@"
|
221 |
|
|
return $?
|
222 |
|
|
}
|
223 |
xiezhen |
1.1 |
|
224 |
|
|
Usage(){
|
225 |
|
|
echo "Usage: $0 -[options]"
|
226 |
|
|
echo "options:"
|
227 |
|
|
echo "--help show this help message and exit"
|
228 |
xiezhen |
1.4 |
echo "--port port number(default 6789) "
|
229 |
xiezhen |
1.1 |
echo "--cmsswlocation cmssw installation base dir(required)"
|
230 |
|
|
echo "--cmsswversion cmssw version(required)"
|
231 |
xiezhen |
1.2 |
echo "--hostname hostname(default to hostname)"
|
232 |
xiezhen |
1.1 |
}
|
233 |
|
|
|
234 |
|
|
setcmsswenv(){
|
235 |
|
|
here=`pwd`
|
236 |
|
|
cd ${CMSSWLOCATION}/${CMSSWVERSION}
|
237 |
|
|
eval `scramv1 runtime -sh`
|
238 |
|
|
cd $here
|
239 |
|
|
}
|
240 |
|
|
E_OPTERR=65
|
241 |
xiezhen |
1.2 |
|
242 |
|
|
PORT=6789
|
243 |
|
|
CMSSWLOCATION=
|
244 |
|
|
CMSSWVERSION=
|
245 |
|
|
MYHOSTNAME=$HOSTNAME
|
246 |
|
|
while getoptex "help; hostname: port: cmsswlocation: cmsswversion: hostname: " "$@"
|
247 |
xiezhen |
1.1 |
do
|
248 |
|
|
case "$OPTOPT" in
|
249 |
|
|
"help") Usage; exit 0;;
|
250 |
|
|
"port") PORT=$OPTARG;;
|
251 |
|
|
"cmsswlocation") CMSSWLOCATION=$OPTARG ;;
|
252 |
|
|
"cmsswversion") CMSSWVERSION=$OPTARG;;
|
253 |
xiezhen |
1.2 |
"hostname") MYHOSTNAME=$OPTARG ;;
|
254 |
xiezhen |
1.1 |
*) break ;;
|
255 |
|
|
esac
|
256 |
|
|
done
|
257 |
|
|
if [ -f ../etc/profile.d/init.sh ]
|
258 |
|
|
then
|
259 |
|
|
source ../etc/profile.d/init.sh
|
260 |
|
|
fi
|
261 |
|
|
echo "============================================"
|
262 |
|
|
echo starting server with the following parameters
|
263 |
|
|
echo "PORT $PORT"
|
264 |
|
|
echo "CMSSWVERSION $CMSSWVERSION"
|
265 |
|
|
echo "CMSSWLOCATION $CMSSWLOCATION"
|
266 |
xiezhen |
1.2 |
echo "HOSTNAME $MYHOSTNAME"
|
267 |
xiezhen |
1.1 |
echo "============================================"
|
268 |
|
|
|
269 |
|
|
setcmsswenv
|
270 |
xiezhen |
1.2 |
python ./server.py --hostname "$MYHOSTNAME" --port "$PORT"
|
271 |
|
|
exit 0
|