Solved

Korn Shell Substring

Posted on 2006-06-14
3
3,795 Views
Last Modified: 2013-12-26
In a korn shell script I have a directory name in a variable $TARGET_DIR

e.g. "/home/username" or "/home/username/"

I want to ensure that the string is in the second format (with the trailing slash)

How can this be done with ksh?

I was going to do it something like this:

#get the length of the string
STR_LEN=${#$TARGET_DIR}

#Get the last character in the string
LST_CHR=substr($TARGET_DIR, $STR_LEN - 1)   ??? This part is made up and not a real command.

#If the last char isn't "/"  then add it to the end
if [ $LST_CHR != '/' ]
then
  TARGET_DIR=${TARGET_DIR}/
fi

Get it?  Since I'm obviously new to ksh if you have any little syntax corrections for the rest of my code that would be helpful too.

Thanks!
0
Comment
Question by:aflat362
3 Comments
 
LVL 3

Accepted Solution

by:
aflat362 earned 0 total points
Comment Utility
NEVERMIND!

I figured out another way to do this.

TARGET_DIR=${TARGET_DIR%/}  #remove trailing slash if it exists
TARGET_DIR=${TARGET_DIR}/   #add the slash to the end

Mods - please delete this question.
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
if u want this question deleted, u have to post the request in community support
provide the link to this question
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 87
countEvens challenge 2 57
ShiftLeft challenge 21 65
noX challenge 17 76
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now