On Sat, 18 Apr 2009, walter harms wrote:

> you are right error handling is important
> may be can redefine croak() to
>
>
> error() {
>        echo "$tag: $*" >&2
>        exit 1
> }

Right.  See attached.

> additional points for having logger: (maybe this is to much but i am
> not sure)
>
> if [ -x /usr/bin/logger ]
> then
>       ECHO=/usr/bin/logger -s -t $(basename $0)
> else
>       ECHO=echo
> fi

Maybe.  But this is simplier:

ECHO=echo
! type logger >/dev/null 2>&1 || ECHO="logger -s -t$tag"

By the way, $(basename $0) is more expensive than ${0##*/}.


Cheers,

-- 
Cristian
--- busybox-svn/examples/udhcp/simple.script	(revision 26144)
+++ busybox-svn/examples/udhcp/simple.script	(working copy)
@@ -2,45 +2,63 @@
 
 # udhcpc script edited by Tim Riker <[email protected]>
 
-[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
+tag="${0##*/}[$$]"
 
-RESOLV_CONF="/etc/resolv.conf"
-NETMASK=""
-[ -n "$subnet" ] && NETMASK="netmask $subnet"
+croak() {
+	echo "$tag: $*" >&2
+}
+
+whine() {
+	croak "$tag: $*"
+	exit 1
+}
+
+[ $# -gt 0 ] && [ "$1" ] || whine "missing or empty argument(s)"
+
+RESOLV_CONF=/etc/resolv.conf
+NETMASK=
+[ -z "$subnet" ] || NETMASK="netmask $subnet"
 BROADCAST="broadcast +"
-[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
+[ -z "$broadcast" ] || BROADCAST="broadcast $broadcast"
 
 case "$1" in
 	deconfig)
 		echo "Setting IP address 0.0.0.0 on $interface"
-		ifconfig $interface 0.0.0.0
+		ifconfig $interface 0.0.0.0 || whine "failed to $1 $interface"
 		;;
 
 	renew|bound)
 		echo "Setting IP address $ip on $interface"
-		ifconfig $interface $ip $NETMASK $BROADCAST
+		ifconfig $interface $ip $NETMASK $BROADCAST ||
+			whine "failed to $1 $interface"
 
-		if [ -n "$router" ] ; then
+		if [ "$router" ]; then
 			echo "Deleting routers"
-			while route del default gw 0.0.0.0 dev $interface ; do
+			while route del default gw 0.0.0.0 dev $interface; do
 				:
 			done
 
 			metric=0
-			for i in $router ; do
-				echo "Adding router $i"
-				route add default gw $i dev $interface metric $((metric++))
+			for r in $router; do
+				echo "Adding router $r"
+				route add default gw $r dev $interface \
+					metric $((metric++)) ||
+				    whine "failed"
 			done
 		fi
 
-		echo "Recreating $RESOLV_CONF"
-		echo -n > $RESOLV_CONF-$$
-		[ -n "$domain" ] && echo search $domain >> $RESOLV_CONF-$$
-		for i in $dns ; do
-			echo " Adding DNS server $i"
-			echo nameserver $i >> $RESOLV_CONF-$$
-		done
-		mv $RESOLV_CONF-$$ $RESOLV_CONF
+		if [ "$dns" ]; then
+			echo "Recreating $RESOLV_CONF"
+			echo -n > $RESOLV_CONF-$$ ||
+				whine "failed; readonly fs?"
+			[ -z "$domain" ] ||
+				echo search $domain >> $RESOLV_CONF-$$
+			for d in $dns; do
+				echo " Adding DNS server $d"
+				echo nameserver $d >> $RESOLV_CONF-$$
+			done
+			mv -f $RESOLV_CONF-$$ $RESOLV_CONF
+		fi
 		;;
 esac
 
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to