how to know a string inside another string

how to know a string inside another string

Post by root » Mon, 11 Feb 2002 13:20:30



Hi:

I need help.
How to know a string inside another string
for example.
"home" is in "home is mine"

Best Regards

zhengyi

 
 
 

how to know a string inside another string

Post by Ben Altma » Mon, 11 Feb 2002 13:22:35



Quote:> Hi:

> I need help.
> How to know a string inside another string
> for example.
> "home" is in "home is mine"

Ksh:
    if [[ $str = *home* ]]; then
        :
    fi

regards,
Ben

 
 
 

how to know a string inside another string

Post by Chris F.A. Johnso » Mon, 11 Feb 2002 13:40:49





> > Hi:

> > I need help.
> > How to know a string inside another string
> > for example.
> > "home" is in "home is mine"

> Ksh:

And bash:

Quote:>     if [[ $str = *home* ]]; then
>         :
>     fi

ALL Bourne-type shells (including ksh and bash):

        ## to generalize, replace '"home is mine"' with a variable
        case "home is mine" in
           *$str*) echo YES ;;
           *) echo NO ;;
        esac

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

how to know a string inside another string

Post by root » Mon, 11 Feb 2002 13:44:21





> > Hi:

> > I need help.
> > How to know a string inside another string
> > for example.
> > "home" is in "home is mine"

> Ksh:
>     if [[ $str = *home* ]]; then
>         :
>     fi

> regards,
> Ben

thankx a lot
zhengyi
 
 
 

how to know a string inside another string

Post by thekillerbea » Mon, 11 Feb 2002 14:09:34







> > > Hi:

> > > I need help.
> > > How to know a string inside another string
> > > for example.
> > > "home" is in "home is mine"

> > Ksh:

> And bash:

> >     if [[ $str = *home* ]]; then
> >         :
> >     fi

> ALL Bourne-type shells (including ksh and bash):

> ## to generalize, replace '"home is mine"' with a variable
> case "home is mine" in
>    *$str*) echo YES ;;
>    *) echo NO ;;
> esac

why the double square brackets in "if [[ $str = *home* ]]; then"?
 
 
 

how to know a string inside another string

Post by Cyrille MAUCC » Mon, 11 Feb 2002 18:05:40









> > > > Hi:

> > > > I need help.
> > > > How to know a string inside another string
> > > > for example.
> > > > "home" is in "home is mine"

> > > Ksh:

> > And bash:

> > >     if [[ $str = *home* ]]; then
> > >         :
> > >     fi

> > ALL Bourne-type shells (including ksh and bash):

> > ## to generalize, replace '"home is mine"' with a variable
> > case "home is mine" in
> >    *$str*) echo YES ;;
> >    *) echo NO ;;
> > esac

> why the double square brackets in "if [[ $str = *home* ]]; then"?

taken from http://www.kornshell.com/doc/faq.html
---
Q10. What is the difference between [...] and [[...]]?
A10. The [[...]] is processed as part of the shell grammar
 whereas [...] is processed like any other command.
 Operators and operands are detected when the command is
 read, not after expansions are performed.  The shell does not
 do word splitting or pathname generation inside  [[...]].
 This allows patterns to be specified for string matching
 purposes.
---

You cannot use regexp inside single brackets.

Hope it helps
Cyrille

 
 
 

how to know a string inside another string

Post by David Thompso » Wed, 13 Feb 2002 01:12:28



Quote:> I need help.
> How to know a string inside another string
> for example.
> "home" is in "home is mine"

I tend to use expr alot ...

if expr "home is mine" : ".*home.*" > /dev/null ; then
    echo found
else
    echo not found
fi

expr also allows you to use regular expresions.

--
David Thompson

Foster City, CA, USA