check if a variable contains a string

hi

I have an if condition that states:

if [$x does not contain $y]; then
exit

how to translate this?

$x is a path
$y is a string that comes at the end of the path

thx

one way

echo "$x" | grep -q "$y" 
if [[ $? -ne 0 ]] ; then
   exit
fi

If "$y" always comes at the end of "$x" and (you said the content is a path) is preceeded by a slash ("/") you could use the shells variable expansion feature to trim "$x" accordingly and compare if it equals to "$y". "${x##*/}" suppresses every content of "$x" up to the last slash:

x="/a/b/c/d/e"
y="f"                   # comment this, uncomment the following line and run again
# y="e"                 

if [ "${x##*/}" = "$y" ] ; then
     echo "x and y are equal"
else
     echo "x and y are different"
fi

Note that "equal" means just "equal in the mentioned regard".

i hope this helps.

bakunin

hi

the script works fine if x="/a/b/c/d/e"

but the script does not work if x="/a/b/c/d/e/"?

i mean the last slash is after the last string.

any idea?

thanks

If you use bash 3.0 or greater try

if [[ $var =~ "*/$" ]] ; then
   var=$( dirname $var)
fi

else try

echo "$var" | grep -q '*/$'
if [[ $? -eq 0 ] ; then
   var=$(dirname $var)
fi

is there a better solution that gets the last string and removes the trailing slash if it exists?

thanks.

> echo $sample
/a/b/c/d/e
> echo $sample1
/a/b/c/d/e/

> echo $sample | sed "s/\/$//" | awk -F"/" '{print $NF}'
e
> echo $sample1 | sed "s/\/$//" | awk -F"/" '{print $NF}'
e
basename "/a/b/c/d/e"
e

basename "/a/b/c/d/e/"
e

to find the last string on a path, try using basename, something like
basename path.

So the condition will look like

if [ `basename path` -ne $y ]

--Aj

What is a "better" solution? Less clockcycles to run? Less characters to type? Less seconds needed to come up with?

All the mentioned solutions use an external tool (basename, grep, etc.), which means the script has to load a program an run that and only after its completion it can resume its work. This costs some amount of time. If you do it only rarely you will barely notice the difference, if you do it in the middle of a loop which is often executed you will notice a severe difference.

Having said this, here is a solution completely done in shell only - no external tools used:

# x="/a/b/c/d/e"        # uncomment this and comment the next line - no difference
x="/a/b/c/d/e/"
y="f"                   # comment this, uncomment the following line and run again
# y="e"                 

x="${%%x/}"      # remove trailing slash if there is one
if [ "${x##*/}" = "$y" ] ; then
     echo "x and y are equal"
else
     echo "x and y are different"
fi

I hope this helps.

bakunin

Typo ....

x="${%%x/}"      # remove trailing slash if there is one

should be

x="${x%%/}"      # remove trailing slash if there is one

hi

echo "$x" | egrep "AF|GN"
if [[ "$?" -eq 0 ]];
then
echo NO OK
fi

the problem in the above is that it echos $x.

Is there a better if statement that says:

if [ $x contains these characters: AF or GN ]
then
echo NOT OK
fi

thanks