Script Help

Rick Stevens rstevens at vitalstream.com
Wed Nov 8 23:10:43 UTC 2006


On Wed, 2006-11-08 at 15:14 -0700, karlp at ourldsfamily.com wrote:
> Okay, I use bash scripts for my CGI and I have a page I'd like to get "real"
> counts on, so I wrote a script that has a whole bunch of && line extensions.
> 
> I'd like to write a script that could be called with a single environment
> variable assigned a value and have it return either true or false, with
> another variable set with an error or response of some type.
> 
> What would the syntax be to check a bunch of conditions and exit on the first
> match rather than hitting every 'if'
> 
> IE:
> 
> PV=`echo $HTTP_USER_AGENT|grep msnbot` ; processvar
> (or Gigabot or WISEnutbot or inktomisearch, etc.)
> processvar
> 
> where processvar now =
> 
> processvar () {
>  if [ "$PV" = '0' ] ; then ;# control number only
>   cat $FNM | mail -s "Access to $ACCESS" karlp ;# to send email to me
>  else
>   cat $FNM | mail -s "WebBot Access to $ACCESS" karlp
>   PV=0
>  fi
> }
> 
> Currently my syntax results in as many positive hits (and the resultant emails
> about a WebBot) as I have search engines I trace.
> 
> Frankly, I could go back to the previous syntax, but it's very annoying to add
> things to. I'd like to get to the point where I put a search phrase in a file
> and do a   for i in $VAR..do loop

The standard way of forcing an environment variable into a script
(without doing an "export VARNAME") is to put it on the command line
that invokes the script:

	$ VARNAME="data" path-to-script.sh

Then in the script:

	...
	if [ x$(VARNAME) = "xdata"]; then
	   do something
	fi

If you want the shell to export some variable, then use

	declare -x SVARNAME

_IN_ the script to export it.  For example, if "fred.sh" contained:

	#! fred.sh - Show use of variables
	if [ x$(INVAR) = "xdata" ]; then
	    export -x OUTVAR
	    OUTVAR="INVAR was set"
	    exit 0
	else
	    exit 1
	fi

and you ran:

	# INVAR="data" ./fred.sh

Then fred.sh would set a return code of 0 and the environment variable
"OUTVAR" would contain the string "INVAR was set".  If you ran:

	# ./fred.sh

it would return 1 and "OUTVAR" would not be defined.

Note the use of the "x$(INVAR)" construct and the check for "xdata" in
the if statement.  If you don't do that and you don't define INVAR (as
in the second example), you'd get an error because the if would only
have one argument.  By using "x$(name-of-var)" and putting the "x" in
front of whatever data you're looking for, the if statement always has
two arguments.  You can use any characters you want for "x", but "x" is
sorta standard.

----------------------------------------------------------------------
- Rick Stevens, Senior Systems Engineer     rstevens at vitalstream.com -
- VitalStream, Inc.                       http://www.vitalstream.com -
-                                                                    -
-     Warning:  You are logged into reality as the root user...      -
----------------------------------------------------------------------




More information about the Redhat-install-list mailing list