[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How do we get state of a flag in set -o ...
From: |
Greg Wooledge |
Subject: |
Re: How do we get state of a flag in set -o ... |
Date: |
Tue, 11 Jul 2023 18:14:48 -0400 |
On Tue, Jul 11, 2023 at 11:49:40PM +0200, alex xmb ratchev wrote:
> set -- opt1 val1 opt2 val2 ; declare -A conf=( $@ ) ; declare -p conf
> declare -A
> conf=(["opt1 val1 opt2 val2"]="" )
>
> >
> i dont get it
> im suure i used this working maany times
>
> -- why doesnt it split
The parser sees
conf=( $@ )
Now, the parser is not very bright, so it tries to figure out whether
this command should be parsed as
conf=( [key]=value )
or as
conf=( key value )
It makes this decision WITHOUT expanding $@. It doesn't matter what you
put in $@. The parser has already decided.
unicorn:~$ unset -v hash; declare -A hash
unicorn:~$ set -- '[key1]=meow' '[key2]=bowwow'
unicorn:~$ hash=( $@ )
unicorn:~$ declare -p hash
declare -A hash=(["[key1]=meow [key2]=bowwow"]="" )
As we can see here, the parser has decided that it's dealing with
conf=( key value )
where "key" is whatever $@ expands to, and "value" is the empty string,
because there was no second word after the $@ in the command as it was
parsed. (And since there were no square brackets.)
Returning to the original question for a moment, the OP's question was
how to determine the state of shell options like 'vi' that have no
single-letter equivalent. My suggestion was to parse the output of
"set -o". Another answer was to use test -o vi.
Pursuing my answer for a moment, we start by looking at the output of
set -o:
unicorn:~$ set -o
allexport off
braceexpand on
emacs off
errexit off
[...]
vi on
xtrace off
So, how would we parse this to determine whether the 'vi' option is on
or off? Here's one way:
read -r _ state < <(set -o | grep '^vi')
This will store 'on' or 'off' in the state variable.
The "test -o vi" answer may be better than mine.
All the talk of associative arrays and the array=( key value ... )
syntax is just a digression.
- How do we get state of a flag in set -o ..., Budi, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Budi, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Greg Wooledge, 2023/07/11
- Re: How do we get state of a flag in set -o ..., alex xmb ratchev, 2023/07/11
- Re: How do we get state of a flag in set -o ..., alex xmb ratchev, 2023/07/11
- Re: How do we get state of a flag in set -o ..., alex xmb ratchev, 2023/07/11
- Re: How do we get state of a flag in set -o ...,
Greg Wooledge <=
- Re: How do we get state of a flag in set -o ..., alex xmb ratchev, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Greg Wooledge, 2023/07/11
- Re: How do we get state of a flag in set -o ..., alex xmb ratchev, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Greg Wooledge, 2023/07/11
- Re: How do we get state of a flag in set -o ..., alex xmb ratchev, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Grisha Levit, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Greg Wooledge, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Kerin Millar, 2023/07/11
- Re: How do we get state of a flag in set -o ..., alex xmb ratchev, 2023/07/11
- Re: How do we get state of a flag in set -o ..., Kerin Millar, 2023/07/11