• [ $1 != "frps" -a $1 != "frpc" ] vs [[ $1 != "frps" ]] && [[ $1 != "frp

    From hongyi.zhao@gmail.com@21:1/5 to All on Wed Nov 3 22:33:57 2021
    In bash script, both of the following usages are valid:

    [ $1 != "frps" -a $1 != "frpc" ]
    [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]

    Which is preferable?

    Regards,
    HZ

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Josef Moellers@21:1/5 to hongy...@gmail.com on Thu Nov 4 09:13:10 2021
    On 04.11.21 06:33, hongy...@gmail.com wrote:
    In bash script, both of the following usages are valid:

    [ $1 != "frps" -a $1 != "frpc" ]
    [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]

    Which is preferable?

    The one which is most easily understandable.

    In my (40+ years) experience this beats just about EVERTHING ELSE, apart
    from maybe EXTREME performance differences and/or local conventions.
    If you look at the code 3 months from now (or maybe even longer, say
    tomorrow) and STILL understand it, it is fine.

    Josef

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Josef Moellers on Thu Nov 4 10:40:09 2021
    On 04.11.2021 09:13, Josef Moellers wrote:
    On 04.11.21 06:33, hongy...@gmail.com wrote:
    In bash script, both of the following usages are valid:

    [ $1 != "frps" -a $1 != "frpc" ]
    [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]

    Which is preferable?

    Depending on the actual requirements (portability anyone?), probably
    none of these.

    For the first case; POSIX: ">4 arguments: The results are unspecified."
    (BTW: Why did you quote the constant literals but not the variables?)

    If you don't care about portability and standards use [[...]] .

    Many folks forget that we can formulate (even regexp-)comparisons also
    with the [standard] 'case' construct

    case $1 in (frps|frpc) ;; (*) ... ; esac


    The one which is most easily understandable.

    ...and which shows the least surprises (generally and in your context).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to Janis Papanagnou on Thu Nov 4 04:21:25 2021
    On Thursday, November 4, 2021 at 5:40:14 PM UTC+8, Janis Papanagnou wrote:
    On 04.11.2021 09:13, Josef Moellers wrote:
    On 04.11.21 06:33, hongy...@gmail.com wrote:
    In bash script, both of the following usages are valid:

    [ $1 != "frps" -a $1 != "frpc" ]
    [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]

    Which is preferable?
    Depending on the actual requirements (portability anyone?), probably
    none of these.

    For the first case; POSIX: ">4 arguments: The results are unspecified."
    (BTW: Why did you quote the constant literals but not the variables?)

    Negligence or bad habit of mine :-(

    If you don't care about portability and standards use [[...]] .

    Many folks forget that we can formulate (even regexp-)comparisons also
    with the [standard] 'case' construct

    case $1 in (frps|frpc) ;; (*) ... ; esac

    Which type of regexp is supported here?


    The one which is most easily understandable.
    ...and which shows the least surprises (generally and in your context).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to hongy...@gmail.com on Thu Nov 4 12:29:58 2021
    On 04.11.2021 12:21, hongy...@gmail.com wrote:
    On Thursday, November 4, 2021 at 5:40:14 PM UTC+8, Janis Papanagnou wrote:
    On 04.11.2021 09:13, Josef Moellers wrote:
    On 04.11.21 06:33, hongy...@gmail.com wrote:
    In bash script, both of the following usages are valid:

    [ $1 != "frps" -a $1 != "frpc" ]
    [[ $1 != "frps" ]] && [[ $1 != "frpc" ]]

    Which is preferable?
    Depending on the actual requirements (portability anyone?), probably
    none of these.

    For the first case; POSIX: ">4 arguments: The results are unspecified."
    (BTW: Why did you quote the constant literals but not the variables?)

    Negligence or bad habit of mine :-(

    If you don't care about portability and standards use [[...]] .

    Many folks forget that we can formulate (even regexp-)comparisons also
    with the [standard] 'case' construct

    case $1 in (frps|frpc) ;; (*) ... ; esac

    Which type of regexp is supported here?

    Sorry, it's techncally not "regexps" (BRE, ERE) but Shell Patterns.
    (The same that are also supported in the non-standard 'if [[ ... ]]'
    on the right hand side of the comparison operator.)

    Janis



    The one which is most easily understandable.
    ...and which shows the least surprises (generally and in your context).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)