consistent use of curly brackets

This commit is contained in:
Daniel Steglich 2015-03-03 21:07:07 +01:00
parent a2e8a0ec8a
commit f611614c14

View File

@ -34,41 +34,41 @@ doGetOpt()
ignoreCertError=0 ignoreCertError=0
while getopts ":s:d:u:p:I:U:c:b:" opt; do while getopts ":s:d:u:p:I:U:c:b:" opt; do
case $opt in case ${opt} in
s) s)
server=$OPTARG server=${OPTARG}
;; ;;
d) d)
host=$OPTARG host=${OPTARG}
;; ;;
u) u)
user=$OPTARG user=${OPTARG}
;; ;;
p) p)
pass=$OPTARG pass=${OPTARG}
;; ;;
I) I)
if [ "$OPTARG" != "$EMPTYSTRING" ];then if [ "${OPTARG}" != "${EMPTYSTRING}" ];then
ipurl=$OPTARG ipurl=${OPTARG}
else else
ipurl="" ipurl=""
fi fi
;; ;;
U) U)
if [ "$OPTARG" != "$EMPTYSTRING" ];then if [ "${OPTARG}" != "${EMPTYSTRING}" ];then
updateurl=$OPTARG updateurl=${OPTARG}
else else
updateurl="" updateurl=""
fi fi
;; ;;
b) b)
basicauth=$OPTARG basicauth=${OPTARG}
;; ;;
c) c)
ignoreCertError=$OPTARG ignoreCertError=${OPTARG}
;; ;;
\?) \?)
echo "Invalid option: -$OPTARG" >&2 echo "Invalid option: -${OPTARG}" >&2
exit 1 exit 1
;; ;;
esac esac
@ -77,53 +77,53 @@ doGetOpt()
doWriteCFG() doWriteCFG()
{ {
mkdir $CFGDIR 2> /dev/null mkdir ${CFGDIR} 2> /dev/null
#always write to the inactive config - needs to be enabled via "start" command later #always write to the inactive config - needs to be enabled via "start" command later
file=$CFG_disabled local out_file=${CFG_disabled}
#reset the last update time #reset the last update time
echo 0 > $LASTUPDATE echo 0 > ${LASTUPDATE}
#reset the last updated IP #reset the last updated IP
echo "0.0.0.0" > $IPFILE echo "0.0.0.0" > ${IPFILE}
#reset last update (if there is one) #reset last update (if there is one)
rm $STATUSFILE 2> /dev/null rm ${STATUSFILE} 2> /dev/null
#find the interface (always the default gateway interface) #find the interface (always the default gateway interface)
default_interface=`ip route |grep default |awk '{print $5}'` default_interface=`ip route |grep default |awk '{print $5}'`
#store the given options in ez-ipupdate compatible config file #store the given options in ez-ipupdate compatible config file
echo "host=$host" > $file echo "host=${host}" > ${out_file}
echo "server=$server" >> $file echo "server=${server}" >> ${out_file}
echo "user=${user}:${pass}" >> $file echo "user=${user}:${pass}" >> ${out_file}
echo "service-type=gnudip" >> $file echo "service-type=gnudip" >> ${out_file}
echo "retrys=3" >> $file echo "retrys=3" >> ${out_file}
echo "wildcard" >> $file echo "wildcard" >> ${out_file}
#store UPDATE URL params #store UPDATE URL params
echo "POSTURL $updateurl" > $HELPERCFG echo "POSTURL ${updateurl}" > ${HELPERCFG}
echo "POSTAUTH $basicauth" >> $HELPERCFG echo "POSTAUTH ${basicauth}" >> ${HELPERCFG}
echo "POSTSSLIGNORE $ignoreCertError" >> $HELPERCFG echo "POSTSSLIGNORE ${ignoreCertError}" >> ${HELPERCFG}
#check if we are behind a NAT Router #check if we are behind a NAT Router
echo "IPURL $ipurl" >> $HELPERCFG echo "IPURL ${ipurl}" >> ${HELPERCFG}
if [ -z $ipurl ];then if [ -z ${ipurl} ];then
echo "NAT unknown" >> $HELPERCFG echo "NAT unknown" >> ${HELPERCFG}
else else
doGetWANIP doGetWANIP
ISGLOBAL=`ip addr ls $default_interface | grep $wanip` ISGLOBAL=`ip addr ls ${default_interface} | grep ${wanip}`
if [ -z $ISGLOBAL ];then if [ -z ${ISGLOBAL} ];then
#we are behind NAT #we are behind NAT
echo "NAT yes" >> $HELPERCFG echo "NAT yes" >> ${HELPERCFG}
else else
#we are directly connected #we are directly connected
echo "NAT no" >> $HELPERCFG echo "NAT no" >> ${HELPERCFG}
#if this file is added ez-ipupdate will take ip form this interface #if this file is added ez-ipupdate will take ip form this interface
echo "interface=$default_interface" >> $file echo "interface=${default_interface}" >> ${out_file}
#if this line is added to config file, ez-ipupdate will be launched on startup via init.d #if this line is added to config file, ez-ipupdate will be launched on startup via init.d
echo "daemon" >> $file echo "daemon" >> ${out_file}
echo "execute=$0 success" >> $file echo "execute=${0} success" >> ${out_file}
fi fi
fi fi
} }
@ -135,72 +135,69 @@ doReadCFG()
user="" user=""
pass="" pass=""
ipurl="" ipurl=""
file=""
[ -f $CFG_disabled ] && file=$CFG_disabled
[ -f $CFG ] && file=$CFG
if [ ! -z $file ];then if [ ! -z ${cfgfile} ];then
host=`cat $file 2> /dev/null |grep host |cut -d = -f 2` host=`cat ${cfgfile} 2> /dev/null |grep host |cut -d = -f 2`
server=`cat $file 2> /dev/null |grep server |cut -d = -f 2 |grep -v ^\'\'` server=`cat ${cfgfile} 2> /dev/null |grep server |cut -d = -f 2 |grep -v ^\'\'`
user=`cat $file 2> /dev/null |grep user |cut -d = -f 2 |cut -d : -f 1 ` user=`cat ${cfgfile} 2> /dev/null |grep user |cut -d = -f 2 |cut -d : -f 1 `
pass=`cat $file 2> /dev/null |grep user |cut -d = -f 2 |cut -d : -f 2` pass=`cat ${cfgfile} 2> /dev/null |grep user |cut -d = -f 2 |cut -d : -f 2`
fi fi
if [ ! -z $HELPERCFG ];then if [ ! -z ${HELPERCFG} ];then
ipurl=`cat $HELPERCFG 2> /dev/null |grep ^IPURL |awk '{print $2}' |grep -v ^\'\'` ipurl=`cat ${HELPERCFG} 2> /dev/null |grep ^IPURL |awk '{print $2}' |grep -v ^\'\'`
updateurl=`cat $HELPERCFG 2> /dev/null |grep POSTURL |awk '{print $2}' |grep -v ^\'\'` updateurl=`cat ${HELPERCFG} 2> /dev/null |grep POSTURL |awk '{print $2}' |grep -v ^\'\'`
basicauth=`cat $HELPERCFG 2> /dev/null |grep POSTAUTH |awk '{print $2}' |grep -v ^\'\'` basicauth=`cat ${HELPERCFG} 2> /dev/null |grep POSTAUTH |awk '{print $2}' |grep -v ^\'\'`
ignoreCertError=`cat $HELPERCFG 2> /dev/null |grep POSTSSLIGNORE |awk '{print $2}' |grep -v ^\'\'` ignoreCertError=`cat ${HELPERCFG} 2> /dev/null |grep POSTSSLIGNORE |awk '{print $2}' |grep -v ^\'\'`
fi fi
} }
doStatus() doStatus()
{ {
PROC=`pgrep ${TOOLNAME}` PROC=`pgrep ${TOOLNAME}`
if [ -f $CRONJOB ];then if [ -f ${CRONJOB} ];then
echo enabled echo enabled
elif [ ! -z $PROC ];then elif [ ! -z ${PROC} ];then
echo enabled echo enabled
else else
echo disabled echo disabled
fi fi
if [ ! -z $server ];then if [ ! -z ${server} ];then
echo $server echo ${server}
else else
echo disabled echo disabled
fi fi
if [ ! -z $host ];then if [ ! -z ${host} ];then
echo $host echo ${host}
else else
echo disabled echo disabled
fi fi
if [ ! -z $user ];then if [ ! -z ${user} ];then
echo $user echo ${user}
else else
echo disabled echo disabled
fi fi
if [ ! -z $pass ];then if [ ! -z ${pass} ];then
echo $pass echo ${pass}
else else
echo disabled echo disabled
fi fi
if [ ! -z $ipurl ];then if [ ! -z ${ipurl} ];then
echo $ipurl echo ${ipurl}
else else
echo disabled echo disabled
fi fi
if [ ! -z $updateurl ];then if [ ! -z ${updateurl} ];then
echo $updateurl echo ${updateurl}
else else
echo disabled echo disabled
fi fi
if [ ! -z $basicauth ];then if [ ! -z ${basicauth} ];then
echo $basicauth echo ${basicauth}
else else
echo disabled echo disabled
fi fi
if [ ! -z $ignoreCertError ];then if [ ! -z ${ignoreCertError} ];then
echo $ignoreCertError echo ${ignoreCertError}
else else
echo disabled echo disabled
fi fi
@ -208,12 +205,12 @@ doStatus()
doGetWANIP() doGetWANIP()
{ {
if [ ! -z $ipurl ];then if [ ! -z ${ipurl} ];then
outfile=`mktemp` outfile=$(mktemp)
$WGET $WGETOPTIONS -O $outfile $ipurl ${WGET} ${WGETOPTIONS} -O ${outfile} ${ipurl}
wanip=`cat $outfile` wanip=$(cat ${outfile})
rm $outfile rm ${outfile}
[ -z $wanip ] && wanip=${NOIP} [ -z ${wanip} ] && wanip=${NOIP}
else else
#no WAN IP found because of missing check URL #no WAN IP found because of missing check URL
wanip=${NOIP} wanip=${NOIP}
@ -222,30 +219,33 @@ doGetWANIP()
doUpdate() doUpdate()
{ {
if [ ! -z $server ];then if [ ! -z ${server} ];then
start-stop-daemon -S -x ${UPDATE_TOOL} -m -p ${PIDFILE} -- -c $file start-stop-daemon -S -x ${UPDATE_TOOL} -m -p ${PIDFILE} -- -c ${cfgfile}
fi fi
if [ ! -z $updateurl ];then if [ ! -z ${updateurl} ];then
echo "todo" echo "todo"
fi fi
} }
cmd=$1 cmd=${1}
shift shift
case $cmd in cfgfile="/tmp/none"
[ -f ${CFG_disabled} ] && cfgfile=${CFG_disabled}
[ -f ${CFG} ] && cfgfile=${CFG}
case ${cmd} in
configure) configure)
doGetOpt $@ doGetOpt ${@}
doWriteCFG doWriteCFG
;; ;;
start) start)
if [ "$(cat $HELPERCFG |grep ^NAT | awk '{print $2}')" = "no" ];then if [ "$(cat $HELPERCFG |grep ^NAT | awk '{print $2}')" = "no" ];then
if [ -f $CFG -a ! -z $(cat $file 2> /dev/null |grep server |cut -d = -f 2 |grep -v ^\'\')];then if [ -f ${CFG} -a ! -z $(cat ${cfgfile} 2> /dev/null |grep server |cut -d = -f 2 |grep -v ^\'\')];then
mv $CFG_disabled $CFG mv ${CFG_disabled} ${CFG}
/etc/init.d/${TOOLNAME} start /etc/init.d/${TOOLNAME} start
fi fi
if [ ! -z $(cat $HELPERCFG |grep ^POSTURL | awk '{print $2}') ];then if [ ! -z $(cat $HELPERCFG |grep ^POSTURL | awk '{print $2}') ];then
echo "*/${UPDATEMINUTES} * * * * root $0 update" > $CRONJOB echo "*/${UPDATEMINUTES} * * * * root $0 update" > ${CRONJOB}
$0 update $0 update
fi fi
else else
@ -255,20 +255,17 @@ case $cmd in
;; ;;
get-nat) get-nat)
NAT=`cat $HELPERCFG 2> /dev/null |grep ^NAT | awk '{print $2}'` NAT=`cat $HELPERCFG 2> /dev/null |grep ^NAT | awk '{print $2}'`
[ -z $NAT ] && NAT="unknown" [ -z ${NAT} ] && NAT="unknown"
echo $NAT echo ${NAT}
;; ;;
update) update)
doReadCFG doReadCFG
oldip=`cat $IPFILE` oldip=`cat ${IPFILE}`
doGetWANIP doGetWANIP
echo $wanip > $IPFILE echo ${wanip} > ${IPFILE}
cfgfile="/tmp/none" cat ${cfgfile} |grep -v execute > ${cfgfile}.tmp
[ -f $CFG_disabled ] && cfgfile=$CFG_disabled
[ -f $CFG ] && cfgfile=$CFG
cat $file |grep -v execute > ${cfgfile}.tmp
mv ${cfgfile}.tmp ${cfgfile} mv ${cfgfile}.tmp ${cfgfile}
echo "execute=$0 success ${wanip}" >> $cfgfile echo "execute=${0} success ${wanip}" >> ${cfgfile}
#if we know our WAN IP, only update if IP changes #if we know our WAN IP, only update if IP changes
if [ "${oldip}" != "${wanip}" -a "${wanip}" != ${NOIP} ];then if [ "${oldip}" != "${wanip}" -a "${wanip}" != ${NOIP} ];then
doUpdate doUpdate
@ -277,39 +274,39 @@ case $cmd in
if [ "${wanip}" = ${NOIP} ];then if [ "${wanip}" = ${NOIP} ];then
uptime=`cat /proc/uptime |cut -d . -f 1` uptime=`cat /proc/uptime |cut -d . -f 1`
LAST=0 LAST=0
[ -f ${LASTUPDATE} ] && LAST=`cat $LASTUPDATE` [ -f ${LASTUPDATE} ] && LAST=`cat ${LASTUPDATE}`
diff=`expr $uptime - $LAST` diff=`expr ${uptime} - ${LAST}`
if [ $diff -gt $UPDATEMINUTESUNKNOWN ];then if [ ${diff} -gt ${UPDATEMINUTESUNKNOWN} ];then
doUpdate doUpdate
fi fi
fi fi
;; ;;
stop) stop)
rm $CRONJOB 2> /dev/null rm ${CRONJOB} 2> /dev/null
/etc/init.d/${TOOLNAME} stop /etc/init.d/${TOOLNAME} stop
kill $(cat ${PIDFILE}) 2> /dev/null kill $(cat ${PIDFILE}) 2> /dev/null
mv $CFG $CFG_disabled mv ${CFG} ${CFG_disabled}
;; ;;
success) success)
date=`date` date=`date`
echo "last update done ($date)" > $STATUSFILE echo "last update done (${date})" > ${STATUSFILE}
cat /proc/uptime |awk '{print $1}' |cut -d . -f 1 > $LASTUPDATE cat /proc/uptime |awk '{print $1}' |cut -d . -f 1 > ${LASTUPDATE}
#if called from cronjob, the current IP is given as parameter #if called from cronjob, the current IP is given as parameter
if [ $# -eq 1 ];then if [ $# -eq 1 ];then
echo $1 > $IPFILE echo ${1} > ${IPFILE}
else else
#if called from ez-ipupdate daemon, no WAN IP is given as parameter #if called from ez-ipupdate daemon, no WAN IP is given as parameter
doGetWANIP doGetWANIP
echo $wanip > $IPFILE echo ${wanip} > ${IPFILE}
fi fi
;; ;;
failed) failed)
date=`date` date=`date`
echo "last update failed ($date)" > $STATUSFILE echo "last update failed (${date})" > ${STATUSFILE}
;; ;;
get-last-success) get-last-success)
if [ -f $STATUSFILE ];then if [ -f ${STATUSFILE} ];then
cat $STATUSFILE cat ${STATUSFILE}
else else
echo "no successful update recorded since last config change" echo "no successful update recorded since last config change"
fi fi
@ -319,11 +316,11 @@ case $cmd in
doStatus doStatus
;; ;;
get-timer) get-timer)
echo $UPDATEMINUTES echo ${UPDATEMINUTES}
;; ;;
clean) clean)
rm ${CFGDIR}/* rm ${CFGDIR}/*
rm $CRONJOB rm ${CRONJOB}
;; ;;
*) *)
echo "usage: status|configure <options>|start|stop|update|get-nat|clean|success [updated IP]|failed" echo "usage: status|configure <options>|start|stop|update|get-nat|clean|success [updated IP]|failed"