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)

eitama 2010-03-11 16:23

[Linux bash] Why is this not working?
 
I've been at this for 3 hours now.
It's not wrorking.

Code:

#!/bin/sh

#my=`/sbin/ifconfig usb0`
my="abcd"

if [ $my == "*bc*" ]
then
        echo "Ok"
else
        echo "Not ok"
fi

This prints "Not ok" when i would expect it to print Ok.

I ran this on my phone using putty.

On the other hand :

this works :

Code:

#!/bin/sh

#my=`/sbin/ifconfig usb0`
my="abcd"

if [ $my == "abcd" ]
then
        echo "Ok"
else
        echo "Not ok"
fi

Please help :confused:

gobuki 2010-03-11 16:31

Re: [Linux bash] Why is this not working?
 
I didn't know that * is working like that with strings. Is it? It's expanded to filesystem objects names in bash.

If you are sure this should work. How about using #!/bin/bash? :-)

eitama 2010-03-11 16:41

Re: [Linux bash] Why is this not working?
 
As far as I know, glob works with == yes,

But, there is no bash on the N900, busybox....

fatalsaint 2010-03-11 16:41

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

#!/bin/sh

#my=`/sbin/ifconfig usb0`
my="abcd"

if [ "$my" == *bc* ]
then
        echo "Ok"
else
        echo "Not ok"
fi

Maybe?

eitama 2010-03-11 16:45

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

Originally Posted by fatalsaint (Post 563982)
Code:

#!/bin/sh

#my=`/sbin/ifconfig usb0`
my="abcd"

if [ "$my" == *bc* ]
then
        echo "Ok"
else
        echo "Not ok"
fi

Maybe?

Doesn't matter, still won't work.
The thing that is not working is the * (asterisks).

What I am ultimately trying to do is check if usb0 is RUNNING.

JosefA 2010-03-11 16:46

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

#!/bin/bash

my="abcd"

if [[ $my = *bc* ]]
then
        
echo "Ok"
else
        echo 
"Not ok"
fi 


eitama 2010-03-11 16:48

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

Originally Posted by JosefA (Post 563993)
PHP Code:

#!/bin/bash

my="abcd"

if [[ $my = *bc* ]]
then
        
echo "Ok"
else
        echo 
"Not ok"
fi 


I don't have BASH on my N900.

kopele 2010-03-11 16:48

Re: [Linux bash] Why is this not working?
 
Comparing strings like this is a form of regex which requires you use double square brackets around it: if [[ ]]

JosefA 2010-03-11 16:51

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

fatalsaint 2010-03-11 16:53

Re: [Linux bash] Why is this not working?
 
Oops.. I forgot the double brackets.

I knew the lack of quotes on the *bc* though... ;)

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.

rcull 2010-03-11 19:23

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

Code:

if /sbin/ifconfig usb0 > /dev/null 2>&1
then
    echo Running
else
    echo Not Running
fi

Rick

lma 2010-03-11 20:03

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

Originally Posted by eitama (Post 563990)
The thing that is not working is the * (asterisks).

Try a pattern matching expression, like this:
Code:

if [ "${my#*bc}" != "$my" ]

gobuki 2010-03-11 20:22

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

Originally Posted by lma (Post 564241)
Try a pattern matching expression, like this:
Code:

if [ "${my#*bc}" != "$my" ]

Just to mention it: this is a bash feature and not supported by standard shells/busybox.
You should really install a shell with better scripting support if you need regex and follow the countless shell-scripting tutorials on the net - unless maybe you want to deploy it elsewhere. Just make sure you're reading a tutorial for the right shell, since shell scripting != shell scripting.

lma 2010-03-12 05:38

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

Originally Posted by gobuki (Post 564266)
Just to mention it: this is a bash feature and not supported by standard shells/busybox.

Wrong, try it!

gobuki 2010-03-12 12:32

Re: [Linux bash] Why is this not working?
 
You are right! I tried it and it works. I'm sorry. But I wonder where this is documented. I had a look at busybox.net and couldn't find anything about scripting or regular expressions. Not even in the manual page: http://busybox.net/downloads/BusyBox.html

Care to give a keyword or link?

SubCore 2010-03-12 13:39

Re: [Linux bash] Why is this not working?
 
the shell built into busybox is the "Almquist shell" (or, more precisely, the debian ash variant.)

your best bet for documentation would probably be this, that's the posix documentation for -compatible shells.

CowboyFromHell 2010-03-13 01:14

Re: [Linux bash] Why is this not working?
 
If you run linux on your PC and ash is installed:
> man ash
contains all of it. Unfortunately man and manpages are not preinstalled on N900 :°(


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

vBulletin® Version 3.8.8