Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Korn Shell Substring

Posted on 2006-06-14
3
Medium Priority
?
3,854 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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