maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   [Linux bash] Why is this not working? (https://talk.maemo.org/showthread.php?t=47136)

TNiga 2010-03-11 16:53

Re: [Linux bash] Why is this not working?
 
Quote:

Originally Posted by eitama (Post 563998)
I don't have BASH on my N900.

Why don't you install it then?

eitama 2010-03-11 16:55

Re: [Linux bash] Why is this not working?
 
Quote:

Originally Posted by JosefA (Post 564005)
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,

eitama 2010-03-11 16:56

Re: [Linux bash] Why is this not working?
 
Quote:

Originally Posted by TNiga (Post 564007)
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).

CowboyFromHell 2010-03-11 17:24

Re: [Linux bash] Why is this not working?
 
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!

kwotski 2010-03-11 17:32

Re: [Linux bash] Why is this not working?
 
Not sure if this is the *best* way to do it but it does seem to work on my N900:

Quote:

#!/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! :)

eitama 2010-03-11 17:34

Re: [Linux bash] Why is this not working?
 
Quote:

Originally Posted by CowboyFromHell (Post 564047)
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.

CowboyFromHell 2010-03-11 17:47

Re: [Linux bash] Why is this not working?
 
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"

gobuki 2010-03-11 17:53

Re: [Linux bash] Why is this not working?
 
Quote:

Originally Posted by eitama (Post 563979)
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.

CowboyFromHell 2010-03-11 17:55

Re: [Linux bash] Why is this not working?
 
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.

gobuki 2010-03-11 18:04

Re: [Linux bash] Why is this not working?
 
Quote:

Originally Posted by eitama (Post 564064)
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.


All times are GMT. The time now is 12:39.

vBulletin® Version 3.8.8