The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check if a variable contains a . gekkos Shell Programming and Scripting 3 11-27-2008 03:00 PM
how to check string of character manas_ranjan Shell Programming and Scripting 9 05-12-2008 07:20 AM
read string, check string length and cut ozzy80 Shell Programming and Scripting 9 03-21-2007 05:56 PM
check whether variable number or string rolex.mp UNIX for Dummies Questions & Answers 4 02-22-2007 12:12 AM
How to check a string in the variable josephwong Shell Programming and Scripting 1 06-26-2006 12:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-11-2008
melanie_pfefer melanie_pfefer is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 234
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
  #2 (permalink)  
Old 12-11-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
one way
Code:
echo "$x" | grep -q "$y" 
if [[ $? -ne 0 ]] ; then
   exit
fi
  #3 (permalink)  
Old 12-11-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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:

Code:
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
  #4 (permalink)  
Old 01-10-2009
melanie_pfefer melanie_pfefer is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 234
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
  #5 (permalink)  
Old 01-11-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
If you use bash 3.0 or greater try
Code:
if [[ $var =~ "*/$" ]] ; then
   var=$( dirname $var)
fi
else try
Code:
echo "$var" | grep -q '*/$'
if [[ $? -eq 0 ] ; then
   var=$(dirname $var)
fi
  #6 (permalink)  
Old 01-12-2009
melanie_pfefer melanie_pfefer is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 234
is there a better solution that gets the last string and removes the trailing slash if it exists?

thanks.
  #7 (permalink)  
Old 01-12-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Here is another approach

Code:
> 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
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0