help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: whats wrong with (( a = 8 , a > 4 && a -= 2 || a-- )) , bash: ((: a


From: alex xmb ratchev
Subject: Re: whats wrong with (( a = 8 , a > 4 && a -= 2 || a-- )) , bash: ((: a = 8 , a > 4 && a -= 2 || a-- : attempted assignment to non-variable (error token is "-= 2 || a-- ")
Date: Thu, 23 Mar 2023 12:50:58 +0100

On Thu, Mar 23, 2023, 12:38 alex xmb ratchev <fxmbsw7@gmail.com> wrote:

>
>
> On Thu, Mar 23, 2023, 12:21 Kerin Millar <kfm@plushkava.net> wrote:
>
>> On Thu, 23 Mar 2023 11:32:33 +0100
>> alex xmb ratchev <fxmbsw7@gmail.com> wrote:
>>
>> > i remember doing && (( code
>> > maybe i didnt '=' in action there
>> >
>> > (( a = 8 , a > 4 && a -= 2 || a-- ))
>> >
>> > bash: ((: a = 8 , a > 4 && a -= 2 || a-- : attempted assignment to
>> > non-variable (error token is "-= 2 || a-- ")
>> >
>> >
>> > (( a = 8 , a > 4 && a-- && a-- || a-- ))
>> >
>> > works
>> > a=6
>> >
>> > ..
>> > i suppose this is a (( lex bug where u didnt include || && in op for =
>>
>> I agree that it looks like a possible bug in the parser. It works if
>> parentheses are used to disambiguate one of the subtractions.
>>
>> $ (( a = 8 , a > 4 && (a -= 2) || a-- )); declare -p a
>> declare -- a="6"
>>
>> Even when made to work, I would consider it to be poor. Consider what
>> happens below. It is a fine example of what can happen in the course of
>> writing unconventional code without understanding the consequences.
>>
>
> i extra >4 to prevent minus
>
> $ (( a = 2 , a > 1 && (a -= 2) || a-- )); declare -p a
>> declare -- a="-1"
>>
>> If you must do this sort of thing, use the conditional operator instead
>> e.g. (( a = 8, a = (a > 4) ? a - 2 : a - 1 )). Alternatively, just avoid
>> doing assignments in arithmetic expressions except where there's a clear
>> advantage (I cannot see one in this case).
>>
>
> ill do such yes , ? and : stead && and || works also
> .. or similiar .. im fiddeling
> did this
>
>  ((
> cpuc >4 ? cpuc -=2 : cpuc >1 ? cpuc-- : 0
>  ))
>
>>
> the adv is 3 small cmds in one cmd , not 3 separate
> i ve seen massive speed up ment of just chaining var=val assignments ,
> instead of ( nonsensissily ) ; or \n
>

this is my one cmd there

cpuc() {
declare -gi cpuc
 ((
! cpuc ? cpuc = $( 2>&- grep -c ^processor /proc/cpuinfo ) : 0 ,
cpuc >4 ? cpuc -=2 : cpuc >1 ? cpuc-- : ( cpuc =1 )
 ))
  }

no idea about speeds , .. if i find out its slower than the 'normal' way
ill switch

thank you mate
>
> --
>> Kerin Millar
>>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]