Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3860
  • Last Modified:

Korn Shell Substring

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
aflat362
Asked:
aflat362
1 Solution
 
aflat362Author Commented:
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
 
rockiroadsCommented:
if u want this question deleted, u have to post the request in community support
provide the link to this question
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now