Subject: Re: Possible design of the new rc.d rc.conf stuff.
To: None <erh@nimenees.com>
From: Greywolf <greywolf@starwolf.com>
List: current-users
Date: 04/18/2000 19:35:44
On Tue, 18 Apr 2000 erh@nimenees.com wrote:

# > "eval is your friend."
# > 
# > Telling if a variable is done in two places is easily warned about,
# > actually.
# > 
# > Telling which one came first might be a tad more difficult.
# 	hmmm.. I'm trying to think but nothing's happening.  Well nothing
# that I consider reasonable anyway.  How can I use eval to figure out if
# a variable is set in two scripts without either having the scripts do
# the check when setting the variable or knowing the name of the variables
# and checking after reading each script in?
# 
# > If we can make it a flexible solution, we all win.  I'm willing to contribute
# > some effort to it if needed - let me know.
# 	Can you provide an example of the eval thing?

Here's what I've used in the past; doubtless, someone will come up with
a better way.


=== .var.subr === cut here --------
#
# subr.vars:  variable subroutines for setting, unsetting and testing
# variables.
########
#
# setv: set variable
# usage: setv varname value
#
setv () {
	var=$1;
	shift;
	val="$@";

	eval "$var='$val'";
}

#
# unsetv: unset variable
# usage: unsetv var [ var ... ]
#
unsetv () {
	for var in $*; do {
	    eval unset $var;
	} done;
}

#
# printv: print variable value ONLY
# usage: printv var
printv () {
	var=$1;
	shift;
	val=`eval echo '\$'"$var"`;
	echo $val;
}

#
# vprintv: print variable="value"
# usage: vprintv var
#
vprintv () {
	var=$1;
	shift;
	val=`eval echo '\$'"$var"`;
	echo $var=\"$val\";
}

#
# isdef: test a variable's defined state.
# Returns 1 if the variable is not defined, 0 if the variable is defined,
#	possibly empty.
#
# usage: isdef var
#
isdef () {
	var=$1;
	shift;
	def=`eval echo '\${'"$var"':-"NotDEF"'}`;
	case $def in #( <- balance closing paren, below, for show-match
	NotDEF)
		return 1;
		;;
	esac
	return 0;
}
----- cut here ----

				--*greywolf;
--
BSD: it's not free beer, but it's free.