Solved

Korn Shell Substring

Posted on 2006-06-14
3
3,810 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 51
Doc'in system (example?) BA 7 89
firstChar challenge 13 110
Way to decrease size of apk file 9 45
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

895 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

14 Experts available now in Live!

Get 1:1 Help Now