The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to find out pathname from inode number axes UNIX for Advanced & Expert Users 3 09-26-2008 06:44 AM
display pathname of the login shell nadman123 Shell Programming and Scripting 1 04-14-2008 04:15 PM
how to get the last dir from a pathname using IFS sam2004 UNIX for Dummies Questions & Answers 2 03-30-2005 07:34 PM
cpio restore - relative pathname sureshy UNIX for Dummies Questions & Answers 4 08-27-2002 06:28 AM
find without pathname MBGPS UNIX for Dummies Questions & Answers 3 06-27-2002 10:03 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-14-2005
Registered User
 

Join Date: May 2005
Posts: 48
Getting pathname variables with ksh

With C Shell you can get the root, head, tail and extension of a pathname by using pathname variable modifiers.

Example Script:

#! /bin/csh
set pathvar=/home/WSJ091305.txt
echo $pathvar:r
echo $pathvar:h
echo $pathvar:t
echo $pathvar:e

The result of executing this script is:

/home/WSJ091305
/home
WSJ091305.txt
txt

My question is: How can this be done using ksh (I specifically need to get the extension)?

Thanks in advance for the help.
Reply With Quote
Forum Sponsor
  #2  
Old 09-14-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by BCarlson
With C Shell you can get the root, head, tail and extension of a pathname by using pathname variable modifiers.

Example Script:

#! /bin/csh
set pathvar=/home/WSJ091305.txt
echo $pathvar:r
echo $pathvar:h
echo $pathvar:t
echo $pathvar:e

The result of executing this script is:

/home/WSJ091305
/home
WSJ091305.txt
txt

My question is: How can this be done using ksh (I specifically need to get the extension)?

Thanks in advance for the help.
Code:
#!/bin/ksh

pathvar='/home/WSJ091305.txt'
echo "r->[${pathvar%%.*}]"
echo "h->[${pathvar%/*}]"
echo "t->[${pathvar##*/}]"
echo "e->[${pathvar#*.}]"
Reply With Quote
  #3  
Old 09-14-2005
Registered User
 

Join Date: May 2005
Posts: 48
Quote:
Originally Posted by vgersh99
Code:
#!/bin/ksh

pathvar='/home/WSJ091305.txt'
echo "r->[${pathvar%%.*}]"
echo "h->[${pathvar%/*}]"
echo "t->[${pathvar##*/}]"
echo "e->[${pathvar#*.}]"
Now I'm getting this:

r->[/home/WSJ091305]
h->[/home]
t->[WSJ091305.txt]
e->[txt]

Is there a way to only get the results as with the csh?
Reply With Quote
  #4  
Old 09-14-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
well.... it's for you to see HOW it can be used.
Reply With Quote
  #5  
Old 09-14-2005
Registered User
 

Join Date: May 2005
Posts: 48
Will do.

Quote:
Originally Posted by vgersh99
well.... it's for you to see HOW it can be used.
Thanks for the assist. I appreciate it.
Reply With Quote
  #6  
Old 09-16-2005
Registered User
 

Join Date: May 2005
Posts: 48
Thumbs up This worked beautifully.

I was able to get what I needed. Could you point me to some documentation that explains the commands you used to get this? Specifically, what all the characters do? I'm unfamiliar with them. Thanks so much for your help.
Reply With Quote
  #7  
Old 09-16-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Read the manuals.

In man ksh under the section Parameters

In man sh under the section Parameter Expansion
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0