[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: |
alex xmb ratchev |
Subject: |
Re: How do we get state of a flag in set -o ... |
Date: |
Wed, 12 Jul 2023 00:24:38 +0200 |
On Wed, Jul 12, 2023, 12:15 AM Greg Wooledge <greg@wooledge.org> wrote:
> 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.
>
it was "$@" initially .. same effect
however myy biig problem with that story is .. im 100 sure i used such
expandings many
so this all breaks a bit my head
i 100 remember way more -A .. $onevar than mixed multivars usage
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"]="" )
>
i wonder what a=( $@ '' ) say
prolly not the right either
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.)
>
-A doesnt expand {1..3} or ,,,
>
> 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.
>
the test is cool
the set -p parsing for completeness
All the talk of associative arrays and the array=( key value ... )
> syntax is just a digression.
>
ye
>
- 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, 2023/07/11
- Re: How do we get state of a flag in set -o ...,
alex xmb ratchev <=
- 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
- Re: How do we get state of a flag in set -o ..., Chet Ramey, 2023/07/12