[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: setvalue builtin command
From: |
Greg Wooledge |
Subject: |
Re: setvalue builtin command |
Date: |
Mon, 8 Apr 2013 09:05:38 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Fri, Apr 05, 2013 at 09:15:10AM +0800, konsolebox wrote:
> The only thing left here is that we can't have error control like when we
> are to create generally shared library scripts e.g.:
>
> function lib_something {
> declare -n VAR=$1 &>/devnull || { # error message is not suppressed
> : can_t go here if referred variable's name is invalid
> return 1 # or do other things like proper error messaging
> }
> VAR["XYZ.1324"]="Some Value." &>/dev/null || { # error message is not
> suppressed
> : can_t go here if referred variable is not of associative array
> type
> return 1 # or do other things like proper error messaging
> }
> }
For the first part: you can check that "$1" is a valid identifier before
you do the declare.
For the second part: you appear to be correct: you can't reliably
determine what the nameref is pointing to in all cases. If the upstream
variable has a value assigned, then you can; but if it's empty, then you
can't.
imadev:~$ libbar() { declare -n x=$1; declare -p x; }
imadev:~$ unset foo
imadev:~$ libbar foo
bash-4.3: declare: x: not found
imadev:~$ declare -a foo
imadev:~$ libbar foo
bash-4.3: declare: x: not found
imadev:~$ foo=(some stuff)
imadev:~$ libbar foo
declare -a foo='([0]="some" [1]="stuff")'
imadev:~$ declare -A bar
imadev:~$ libbar bar
bash-4.3: declare: x: not found
imadev:~$ bar[x]=q
imadev:~$ libbar bar
declare -A bar='([x]="q" )'
There may be some clever way to determine whether the upstream variable
is an associative array or not, but I can't think of one right now.
Also, please consider using "()" instead of "function" when defining
functions. Bash may support both, but POSIX only requires "()".