[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ${var@A}; hypothetical, related parameter transformations
From: |
Zachary Santer |
Subject: |
Re: ${var@A}; hypothetical, related parameter transformations |
Date: |
Wed, 20 Mar 2024 15:05:56 -0400 |
On Wed, Mar 20, 2024 at 11:27 AM Chet Ramey <chet.ramey@case.edu> wrote:
>
> On 3/19/24 8:56 PM, Zachary Santer wrote:
>
> > So I can get a couple of the things I want by manipulating what I get out
> > of ${var@A} with fairly straightforward parameter expansions. If I needed a
> > declare command and wasn't sure if I would get one, I haven't found a way
> > to expand that to something that is guaranteed to be a declare command
> > maintaining attribute settings. I think you're stuck using an if block at
> > that point.
>
> That expansion will produce a declare command if the variable has any
> attributes, since it needs one to reproduce them. If the variable has a
> value, but no attributes, you'll get an assignment statement. If there
> are no attributes and no value, you get nothing.
I hadn't even considered what to do with a variable that's declared,
but without attributes or value.
Let's say we've got two variables defined in a function like so:
func () {
local -ir number=12
local color='turquoise;
local number_message
generate_declare_command number number_message
local color_message
generate_declare_command color color_message
}
I want a declare command, no matter what ${var@A} gives me. I have to
write a function for that: generate_declare_command (). That function
can work a couple of reasonable ways:
generate_declare_command () {
local -n var="${1}"
local -n message="${2}"
message="${var@A}"
message="${message:-${!var}}"
if [[ ${message} != 'declare -'* ]]; then
message="declare -- ${message}"
fi
}
or
generate_declare_command () {
local -n var="${1}"
local -n message="${2}"
local var_attrs="${var@a}"
message="${var@A}"
message="${message:-${!var}}"
message="declare -${var_attrs:--} ${message#declare * }"
}
Doing this is more work than normalizing the ${var@A} output into an
assignment statement or whatever's on the right hand side of the first
equals sign, each of which can be accomplished with a single parameter
expansion. (Not that either of those would give you anything if the
variable doesn't have a value.) If you want to normalize to any one of
these three things, working with a parameter transformation that could
expand to a declare command or an assignment statement, it's more work
than if there were three separate parameter transformations. one each
to always generate a declare command; an assignment statement; and the
right hand side of a compound assignment statement or standard
assignment statement, depending on whether it's dealing with an
array/assoc or scalar.
Consider that you're talking about changing ${var@A} to expand to a
declare command in the case that the variable has no attributes but is
local to the scope in which the expansion occurs. If you make that
change, then ${var@A} is somewhat unpredictable if you can't assume a
given bash release.
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), (continued)
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Chet Ramey, 2024/03/14
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Zachary Santer, 2024/03/14
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Zachary Santer, 2024/03/14
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Chet Ramey, 2024/03/14
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Zachary Santer, 2024/03/14
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Chet Ramey, 2024/03/18
- ${var@A}; hypothetical, related parameter transformations, Zachary Santer, 2024/03/18
- Re: ${var@A}; hypothetical, related parameter transformations, Zachary Santer, 2024/03/18
- Re: ${var@A}; hypothetical, related parameter transformations, Zachary Santer, 2024/03/19
- Re: ${var@A}; hypothetical, related parameter transformations, Chet Ramey, 2024/03/20
- Re: ${var@A}; hypothetical, related parameter transformations,
Zachary Santer <=
- Re: ${var@A}; hypothetical, related parameter transformations, Greg Wooledge, 2024/03/20
- Re: ${var@A}; hypothetical, related parameter transformations, alex xmb sw ratchev, 2024/03/20
- Re: ${var@A}; hypothetical, related parameter transformations, Zachary Santer, 2024/03/20
- Re: ${var@A}; hypothetical, related parameter transformations, Chet Ramey, 2024/03/21
- Re: ${var@A}; hypothetical, related parameter transformations, Chet Ramey, 2024/03/21
- Re: ${var@A}; hypothetical, related parameter transformations, Zachary Santer, 2024/03/24
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Mike Jonkmans, 2024/03/19
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Chet Ramey, 2024/03/19
- Re: nameref and referenced variable scope, setting other attributes (was "local -g" declaration references local var in enclosing scope), Mike Jonkmans, 2024/03/19
- Re: "local -g" declaration references local var in enclosing scope, Chet Ramey, 2024/03/11