Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    [Linux bash] Why is this not working?

    Reply
    Page 2 of 3 | Prev |   1   2   3   | Next
    TNiga | # 11 | 2010-03-11, 16:53 | Report

    Originally Posted by eitama View Post
    I don't have BASH on my N900.
    Why don't you install it then?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    eitama | # 12 | 2010-03-11, 16:55 | Report

    Originally Posted by JosefA View Post
    The point is, look at the difference between that and your code. The point isn't "do you have bash on your N900". I don't either.
    I already tried the [[ ]], and I can't run it if I leave #!/bin/bash.

    Here :

    Code:
    Nokia-N900-02-8:/etc/network# vi detectUsb.test.sh
    #!/bin/sh
    
    my="abcd"
    
    if [[ $my = *bc* ]]
    then
            echo "Ok"
    else
            echo "Not ok"
    fi
    
    ~
    ~
    
    Nokia-N900-02-8:/etc/network# ./detectUsb.test.sh
    Not ok
    Nokia-N900-02-8:/etc/network#
    Still don't know what I am doing wrong,

    Edit | Forward | Quote | Quick Reply | Thanks

     
    eitama | # 13 | 2010-03-11, 16:56 | Report

    Originally Posted by TNiga View Post
    Why don't you install it then?
    3 Reasons :

    1. I didn't know It's an option.
    2. There are plenty of scripts on the phone that manage without bash,
    i'd like to manage without it as well.
    3. The less apps on my phone the better. (if I can deal without them ofc).

    Edit | Forward | Quote | Quick Reply | Thanks

     
    CowboyFromHell | # 14 | 2010-03-11, 17:24 | Report

    Yep, * has its wildcard characteristic only by certain interpreters. Bash interprets it as gobuki described, as long as it's actually interpreted by bash (i.e. not escaped e.g. by \ or ''). Compare
    echo *b*
    and
    echo "*b*"
    in bash with some file with b in its name in the same directory.
    Thus, fiddeling with * with some tools like
    find ... -regex ... -exec ...
    can be quite tricky in escaping "enough" to avoid bash interpreting it in the first step but have find interpret it for -regex but not for -exec which shall pass it to the next level of bash.........;°))

    For sed and numerous other tools * is the "any number of" operator used in regular expressions ( ab*c).

    Test (or the short form [...]) does not interpret or compare any regular expressions, if using = (single = is the "official" documented version but == works, too) but rather compares the strings literally.

    If you want some regex functionality, try this:

    my="abcd"

    if [ `echo $my | grep bc` ]
    then
    echo "Ok"
    else
    echo "Not ok"
    fi

    Good luck!

    Edit | Forward | Quote | Quick Reply | Thanks

     
    kwotski | # 15 | 2010-03-11, 17:32 | Report

    Not sure if this is the *best* way to do it but it does seem to work on my N900:

    Originally Posted by
    #!/bin/sh

    my="abcd"

    his=`echo "$my" | grep 'bc'`

    if [[ $my == $his ]]

    then
    echo "Ok"
    else
    echo "Not ok"
    fi
    Edit: Meh, beaten to it by a cowboy!

    Edit | Forward | Quote | Quick Reply | Thanks

     
    eitama | # 16 | 2010-03-11, 17:34 | Report

    Originally Posted by CowboyFromHell View Post
    Yep, * has its wildcard characteristic only by certain interpreters. Bash interprets it as gobuki described, as long as it's actually interpreted by bash (i.e. not escaped e.g. by \ or ''). Compare
    echo *b*
    and
    echo "*b*"
    in bash with some file with b in its name in the same directory.
    Thus, fiddeling with * with some tools like
    find ... -regex ... -exec ...
    can be quite tricky in escaping "enough" to avoid bash interpreting it in the first step but have find interpret it for -regex but not for -exec which shall pass it to the next level of bash.........;°))

    For sed and numerous other tools * is the "any number of" operator used in regular expressions ( ab*c).

    Test (or the short form [...]) does not interpret or compare any regular expressions, if using = (single = is the "official" documented version but == works, too) but rather compares the strings literally.

    If you want some regex functionality, try this:

    my="abcd"

    if [ `echo $my | grep bc` ]
    then
    echo "Ok"
    else
    echo "Not ok"
    fi

    Good luck!
    Thank you very much CowboyFromHell,

    The example you posted works well, but when I tried to adapt it a bit to my needs, I have an odd error :

    Code:
    Nokia-N900-02-8:/etc/network# vi detectUsb.sh
    #!/bin/sh
    
    my=`/sbin/ifconfig`
    
    if [ `echo $my | grep RUNNING` ]
    then
            echo "Ok"
    else
            echo "Not ok"
    fi
    
    Nokia-N900-02-8:/etc/network# ./detectUsb.sh
    sh: Link: unknown operand
    Not ok
    Could you help me out on this one?
    I am a big bash / sh noob.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    CowboyFromHell | # 17 | 2010-03-11, 17:47 | Report

    If you only want to check, whether it is running or not, you could do

    if `echo $my | grep -q RUNNING`

    Note: no "test" or [...]. grep -q is "quiet" and gives a return value for "found" or "not found"

    Edit | Forward | Quote | Quick Reply | Thanks

     
    gobuki | # 18 | 2010-03-11, 17:53 | Report

    Originally Posted by eitama View Post
    As far as I know, glob works with == yes,

    But, there is no bash on the N900, busybox....
    Yes, not by default. But you can install it if you need the functionality. I'm using it frequently because i want a command history.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    CowboyFromHell | # 19 | 2010-03-11, 17:55 | Report

    Forgot to mention: This only works for

    my=`/sbin/ifconfig usb0`

    otherwise it would be true if *any* network adapter is RUNNING, as all lines are parsed by grep and it would find some line with RUNNING ;°)

    So, this complete code must work:

    #!/bin/sh

    if `/sbin/ifconfig usb0 2>/dev/null | grep -q RUNNING`
    then
    echo "Ok"
    else
    echo "Not ok"
    fi

    if you replace usb0 by $1 as command line argument, you can use it for any network interface.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by CowboyFromHell; 2010-03-11 at 19:35. Reason: wrong adapter

     
    gobuki | # 20 | 2010-03-11, 18:04 | Report

    Originally Posted by eitama View Post
    Thank you very much CowboyFromHell,

    The example you posted works well, but when I tried to adapt it a bit to my needs, I have an odd error :

    Code:
    Nokia-N900-02-8:/etc/network# vi detectUsb.sh
    #!/bin/sh
    
    my=`/sbin/ifconfig`
    
    if [ `echo $my | grep RUNNING` ]
    then
            echo "Ok"
    else
            echo "Not ok"
    fi
    
    Nokia-N900-02-8:/etc/network# ./detectUsb.sh
    sh: Link: unknown operand
    Not ok
    Could you help me out on this one?
    I am a big bash / sh noob.

    I think the error you made is you are testing the output of grep and not its return value that is in $? after grepping.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by gobuki; 2010-03-11 at 18:05. Reason: typo

     
    Page 2 of 3 | Prev |   1   2   3   | Next
vBulletin® Version 3.8.8
Normal Logout