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 01-12-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,163
Code:
basename "/a/b/c/d/e"
e

basename "/a/b/c/d/e/"
e
  #2 (permalink)  
Old 01-15-2009
KingVikram KingVikram is offline
Registered User
  
 

Join Date: Jan 2008
Location: Virginia
Posts: 3
try basename

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
  #3 (permalink)  
Old 01-15-2009
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Quote:
Originally Posted by melanie_pfefer View Post
is there a better solution that gets the last string and removes the trailing slash if it exists?

thanks.
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:

Code:
# 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
  #4 (permalink)  
Old 01-16-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,912
Typo ....
Code:
x="${%%x/}"      # remove trailing slash if there is one
should be
Code:
x="${x%%/}"      # remove trailing slash if there is one
  #5 (permalink)  
Old 02-02-2009
melanie_pfefer melanie_pfefer is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 234
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
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 08:13 AM.


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