Compare a string



  • Using the following is standard Linux, but does not work for the Onion

    v="Version 0.2.0 b183"
    [[ "$v" == *"b183"* ]] && echo "OK" || echo "FAIL"
    [[ "$v" =~ "b183" ]] && echo "OK" || echo "FAIL"
    

    Both should display "OK", but the first show "FAIL" and the 2nd gives "ash: =~: unknown operand"

    What can I do instead?



  • @Lars-Wassini

    v="Version 0.2.0 b183"
    
    [[ "$v" == *"b183"* ]] && echo "OK" || echo "FAIL"
    # You compare the "Version 0.2.0 b183" string with the "*b183*" string - so they are not equal.
    
    # One of the many options:
    [[ `echo "$v" | grep -o b183` ]] && echo "OK" || echo "FAIL"
    
    
    [[ "$v" =~ "b183" ]] && echo "OK" || echo "FAIL"
    # `=~` ash has no such operand.


  • @György-Farkas said in Compare a string:

    [[ "$v" == *"b183"* ]]

    Using wildcards is working in standard Linux e.g. Ubuntu - but apparently not on the Onion.

    But the "grep" line is working 🙂

    Thanks!



  • @Lars-Wassini said in Compare a string:

    Using wildcards is working in standard Linux e.g. Ubuntu - but apparently not on the Onion.

    Yes. 🙂
    Please install bash - if you can't live without it - and you can use it in bash script in the usual way.
    (You know, the shebang is: #!/bin/bash).

    For example:

    # Omega2+ v0.3.2 b234
    
    root@Omega-99A5:~# opkg update
     ...
    root@Omega-99A5:~# opkg install bash
    Installing bash (4.4.18-2) to root...
    Downloading http://repo.onioniot.com/omega2/packages/packages/bash_4.4.18-2_mipsel_24kc.ipk
    Configuring bash.
    
    # -----------------------------------------------------------------------
    
    root@Omega-99A5:~# cat test.sh
    #!/bin/bash
    
    v="Version 0.2.0 b183"
    
    [[ "$v" == *"b183"* ]] && echo "Test_1: OK" || echo "Test_1: FAIL"
    [[ "$v" =~ "b183" ]] && echo "Test_2: OK" || echo "Test_2: FAIL"
    
    # -----------------------------------------------------------------------
    
    root@Omega-99A5:~# ./test.sh
    Test_1: OK
    Test_2: OK
    


  • @Lars-Wassini just keep in mind that bash is quite large in terms of OpenWrt, since ash runs as part of BusyBox and BusyBox includes a load of other *nix tools yet still fits into roughly 500kb. In contrast Bash alone is around 2mb and you still will have BusyBox installed.



  • @György-Farkas
    That's not an option. I almost already having problems with the size of my own application.
    And I don't want to install Bash on several hundred Onions. 🙂



  • @Lars-Wassini OK. If bash is not an option then you should try to write BusyBox ash compatible scripts.



  • @György-Farkas Yes - thats why I'm using your suggestion using grep. 🙂


Log in to reply
 

Looks like your connection to Community was lost, please wait while we try to reconnect.