Unix Shell - dump list of variables

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > dump list of variables





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author dump list of variables
pottmi@gmail.com

2006-10-22, 1:19 am

I am using bash 3.x and would like a way to one of the following:

1) error if a variable is used that is not declared. (set -o
noundeclare). OK, I already know that that does not exist, but
hopefully that gives you the idea of what I want.

2) dump a list of variables. then I could build my own routine simple
utility to find variables that are not declared.

My goal is to prevent the use of undeclared variables in my code,
during run-time or with a scan.

--
mike.

pottmi@gmail.com

2006-10-22, 1:19 am


pottmi@gmail.com wrote:
> I am using bash 3.x and would like a way to one of the following:
>
> 1) error if a variable is used that is not declared. (set -o
> noundeclare). OK, I already know that that does not exist, but
> hopefully that gives you the idea of what I want.
>
> 2) dump a list of variables. then I could build my own routine simple
> utility to find variables that are not declared.
>
> My goal is to prevent the use of undeclared variables in my code,
> during run-time or with a scan.
>
> --
> mike.


Here is what I have tried:

for a in {a..z} {A..Z}
do
echo ${!$a*} # this line is broken:
done

../dumpvars.sh: line 6: ${!$a*}: bad substitution

I expect to get something like that loop to work, because when I dont
use a variable, I get what I want:

bash-3.1$ echo ${!M*}
MACHTYPE MAILCHECK
bash-3.1$

Chris F.A. Johnson

2006-10-22, 1:19 am

On 2006-10-22, pottmi@gmail.com wrote:
> I am using bash 3.x and would like a way to one of the following:
>
> 1) error if a variable is used that is not declared. (set -o
> noundeclare). OK, I already know that that does not exist, but
> hopefully that gives you the idea of what I want.


: ${var?Not set}

> 2) dump a list of variables. then I could build my own routine simple
> utility to find variables that are not declared.


set

> My goal is to prevent the use of undeclared variables in my code,
> during run-time or with a scan.


set -u

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
William Park

2006-10-22, 1:19 am

pottmi@gmail.com wrote:
> I am using bash 3.x and would like a way to one of the following:
>
> 1) error if a variable is used that is not declared. (set -o
> noundeclare). OK, I already know that that does not exist, but
> hopefully that gives you the idea of what I want.
>
> 2) dump a list of variables. then I could build my own routine simple
> utility to find variables that are not declared.


set

>
> My goal is to prevent the use of undeclared variables in my code,
> during run-time or with a scan.


I've been meaning to add builtin command to test for
- does it exist?
- does it exist and is empty?
but never got around to it.

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Chris F.A. Johnson

2006-10-22, 1:19 am

On 2006-10-22, William Park wrote:
> pottmi@gmail.com wrote:
>
> set
>
>
> I've been meaning to add builtin command to test for
> - does it exist?
> - does it exist and is empty?


Why? What's wrong with the existing parameter expansion operators?


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Kaz Kylheku

2006-10-22, 7:20 am

pottmi@gmail.com wrote:
> I am using bash 3.x and would like a way to one of the following:
>
> 1) error if a variable is used that is not declared. (set -o
> noundeclare).


One way this is done is with the special expansion syntax:

${parameter?word}

If the parameter is not set, then an error message containing word is
displayed. Modify that to :? to check for the parameter being null or
unset.

> OK, I already know that that does not exist, but


A quick scan of the bash 3.0 man page comes up with:

-u Treat unset variables as an error when performing parameter
expansion. If expansion is attempted on an unset variable, the shell
prints an error message, and, if not interactive, exits with a non-zero
status.

Also, elsewhere it is documented that "nounset" is the same as "-u".

The error message betrays the Lisp background of whoever implemented
it:

bash: x: unbound variable

Too bad that the shell has no better way to handle this than to bail
out.

> hopefully that gives you the idea of what I want.


I want static typechecking, classes with an object-store database,
first class functions and lexical closures, and a native compiler!



> 2) dump a list of variables.


Try "set" with no arguments.

pottmi@gmail.com

2006-10-22, 1:16 pm

set -u aka set -o nounset only detects variables that are not set. I
want to detect variables that are not declared using the declare option
in bash. nounset goes a long way to what I want.

What is wrong with the using parameter expansion is that it clutters
code. in my opinion, parameter expansion should be used to validate
user input, not find programming typos.

set with no options looks like it will do part of what I want. What I
intend to do is add a test in our
qa process to validate the variables being used.

William Park

2006-10-22, 1:16 pm

Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
> Why? What's wrong with the existing parameter expansion operators?


How would you do something like (pseudo-code)

if xxx exists, then
print "xxx exists"
else
print "xxx does not exists"

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Chris F.A. Johnson

2006-10-22, 1:16 pm

On 2006-10-22, William Park wrote:
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
> How would you do something like (pseudo-code)
>
> if xxx exists, then
> print "xxx exists"
> else
> print "xxx does not exists"


is_set()
{
eval "[ -n \"\${$1+X}\" ]"
}

if is_set "$1"
then
printf "%s\n" "$1 is set"
else
printf "%s\n" "$1 is not set"
fi


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com