Solved

Korn Shell Substring

Posted on 2006-06-14
3
3,822 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
ID: 16903347
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
ID: 16909948
if u want this question deleted, u have to post the request in community support
provide the link to this question
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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