Code cleanup as reported by shellcheck

This commit is contained in:
Eric Renfro 2017-08-19 19:10:48 -04:00
parent f97da29cee
commit 0f9adac2cd

View file

@ -43,14 +43,12 @@ valid_ip() {
valid_ipv4() { valid_ipv4() {
local ip=$1 local ip=$1
local stat=1 local stat=1
local ipaddr
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS IFS='.' read -ra ipaddr <<< "$ip"
IFS='.' [[ ${ipaddr[0]} -le 255 && ${ipaddr[1]} -le 255 \
ip=($ip) && ${ipaddr[2]} -le 255 && ${ipaddr[3]} -le 255 ]]
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$? stat=$?
fi fi
return $stat return $stat
@ -73,7 +71,7 @@ getRecords() {
if [[ ! -r "$file" ]]; then if [[ ! -r "$file" ]]; then
return 1 return 1
else else
while read r; while read -r r;
do do
r=$(trim "$r") r=$(trim "$r")
[[ ${r:0:1} == '#' ]] && continue [[ ${r:0:1} == '#' ]] && continue
@ -138,11 +136,13 @@ getIPv6() {
getDnsNS() { getDnsNS() {
local rec=$1 local rec=$1
local domain=$(sed 's/[^.]*\.\([^.]*\..*\)/\1/' <<<"$rec") local domain
local result local result
local err local err
result=$(dig +short @8.8.8.8 NS $domain | sed -e 's/.$//') domain=$(sed 's/[^.]*\.\([^.]*\..*\)/\1/' <<<"$rec")
result=$(dig +short @8.8.8.8 NS "$domain" | sed -e 's/.$//')
err=$? err=$?
if [[ $err -eq 0 ]] if [[ $err -eq 0 ]]
@ -167,7 +167,7 @@ getDnsRecord() {
do do
[[ "$i" = "ERROR" ]] && return 1 [[ "$i" = "ERROR" ]] && return 1
result=$(dig +short @${i} ${rectype} ${rec} | head -n1) result=$(dig +short @"$i" "$rectype" "$rec" | head -n1)
err=$? err=$?
if [[ $err -ne 0 ]] || [[ -z "$result" ]]; then if [[ $err -ne 0 ]] || [[ -z "$result" ]]; then
@ -189,7 +189,7 @@ getIPv6Prefix() {
local result local result
local err local err
if valid_ipv6 $rec if valid_ipv6 "$rec"
then then
result=$(echo "$rec" | cut -d':' -f1-4) result=$(echo "$rec" | cut -d':' -f1-4)
result="${result}::/64" result="${result}::/64"
@ -260,12 +260,12 @@ log() {
} }
logerr() { logerr() {
>&2 log $* >&2 log "$@"
} }
run-parts() { run-parts() {
# Ignore *~ and *, scripts # Ignore *~ and *, scripts
for i in $(LC_ALL=C; echo ${1%/}/*[^~,]) ; do for i in $(LC_ALL=C; echo "${1%/}"/*[^~,]) ; do
[[ -d $i ]] && continue [[ -d $i ]] && continue
# Don't run *.{rpmsave,rpmorig,rpmnew,swp,cfsaved} scripts # Don't run *.{rpmsave,rpmorig,rpmnew,swp,cfsaved} scripts
[[ "${i%.cfsaved}" != "${i}" ]] && continue [[ "${i%.cfsaved}" != "${i}" ]] && continue
@ -301,24 +301,25 @@ run-parts() {
run-hook() { run-hook() {
local hook_dir=$1 local hook_dir=$1
local errors=0 local errors=0
local hook_script
shift shift
if [[ ! -d "${hook_dir}" ]]; then if [[ ! -d "${hook_dir}" ]]; then
return 0 return 0
fi fi
while read s while read -r hook_script
do do
log "Running agent: $(basename "$s")" log "Running agent: $(basename "$hook_script")"
DIP_FUNCTIONS="$(readlink -f "$0")" \ DIP_FUNCTIONS="$(readlink -f "$0")" \
DIP_AGENT_NAME="$(basename "$(readlink -f "$s")")" \ DIP_AGENT_NAME="$(basename "$(readlink -f "$hook_script")")" \
DIP_AGENT_EXEC="$(basename "$s")" \ DIP_AGENT_EXEC="$(basename "$hook_script")" \
DIP_AGENT_DIR="$(dirname "$(readlink -f "$s")")" \ DIP_AGENT_DIR="$(dirname "$(readlink -f "$hook_script")")" \
DIP_BASE_DIR="${script_dir}" \ DIP_BASE_DIR="${script_dir}" \
DIP_CUR_IP="$1" \ DIP_CUR_IP="$1" \
DIP_OLD_IP="$2" \ DIP_OLD_IP="$2" \
DIP_RECORD="$3" \ DIP_RECORD="$3" \
"$s" $* "$hook_script" "$@"
err=$? err=$?
if [[ $err -ne 0 ]]; then if [[ $err -ne 0 ]]; then
@ -400,7 +401,7 @@ check-update() {
[[ -z "$iptype" ]] && return 1 [[ -z "$iptype" ]] && return 1
[[ -z "$record" ]] && return 1 [[ -z "$record" ]] && return 1
getCurrentIP $iptype $record getCurrentIP "$iptype" "$record"
err=$? err=$?
case $err in case $err in
@ -427,7 +428,7 @@ check-update() {
logerr "DEBUG: dig output '$externalip'" logerr "DEBUG: dig output '$externalip'"
;; ;;
*) logerr "Unknown fatal error occurred" *) logerr "Unknown fatal error occurred"
eclogerrho "(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }" logerr "(${BASH_SOURCE[0]}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }"
exit $err exit $err
;; ;;
esac esac
@ -445,8 +446,8 @@ prog_lock() {
LOCKFD=99 LOCKFD=99
# PRIVATE # PRIVATE
_lock() { flock -$1 $LOCKFD; } _lock() { flock -"$1" $LOCKFD; }
_no_more_locking() { _lock u; _lock xn && rm -f $LOCKFILE; } _no_more_locking() { _lock u; _lock xn && rm -f "$LOCKFILE"; }
_prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; } _prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; }
# ON START # ON START
@ -473,27 +474,16 @@ prog_lock() {
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Internal Initialization # Internal Initialization
prog_name=$(basename "$0") #prog_name=$(basename "$0")
script_dir=$(dirname "$(readlink -f "$0")") script_dir=$(dirname "$(readlink -f "$0")")
if [[ -z "$1" ]]; then
logerr "ERROR: Need to provide a DNS record or file to look-up"
exit 1
else
if [[ -r "$1" ]]; then
record_file=$1
else
record=$1
fi
fi
prog_lock prog_lock
# Main # Main
if [[ -r "${script_dir}/plugins/${1}" ]]; then if [[ -r "${script_dir}/plugins/${1}" ]]; then
# shellcheck source=plugins/update-ipv4
source "${script_dir}/plugins/${1}" source "${script_dir}/plugins/${1}"
plugin_name=${1,,}
else else
logerr "ERROR: Unknown plugin '${1}'" logerr "ERROR: Unknown plugin '${1}'"
fi fi