Testing the last character in a string

Hi
In the shell scripted I'm trying to write!

I would like to test the last character in a string. The string is a path/directory and I want to see if the last character is a '/'.

The string (path/directory) is inputted by a user. If the '/' character isn't present then I want to be able to append this character to the string.

I started to right this in awk then scrapped it as I thought there must be an easier way?

Thanks in advance

In ksh:
$ a=abcdefg
$ lastchr=${a#${a%?}}
$ echo $lastchr
g
$

Hmmm..rereading the post, it looks like you might just want to ensure that a string ends in slash. If that's the case...

[[ $string != */ ]] && string="$string"/

Appreciate your help

Can I ask you to tell me what each bit of this assignment does...
lastchr=${a#${a%?}}

What does 'a#' and 'a%?' do...

Thanks a lot

ksh has some really arcane substitution syntax. It's kinda fun but a lot of programmers don't know it, just like folks use only 20% of vi's capability.

Because folks here do not know all of that stuff, I try to use 'understandable' versions. Here is the same thing using expr, which is also portable to other shells.

lchr=`expr substr $a ${#a} 1`

the substr operand for expr takes:

$a - string to work on

${#a} - a starting point in the string - in this case the
last character ${#a} is the length of a string.

1 - number of characters after the starting point to return

Generally, if you need a lot of string and array features, try bash.
ksh has most of them but some of it is completely counterintuitive. IMO.

First ${a%<pattern>} strips the pattern off the end of the contents of the variable. And ? is a pattern that matches a single character. So ${a%?} is $a with the last character missing.

And ${a#<pattern>} strips the pattern off of the front of the variable.

Unlike expr, these are shell built-in's. To run expr, you must fork() and then exec(). Staying with shell built-in's delivers real performance benefits.

Assuming you know the syntax - and the guy modifying the script after you also knows :smiley:

There is always a trade off. I often do C code optimizing for others. Optimized C usually looks nothing like what you might think it ought to be -- things like Duff's machine, for example.
I always leave the old understandable stuff behind, buried in comments. For the next guy.

If ksh were truly simple to learn then why did HP(?) come up with keysh? ugh....

Don't get the wrong idea - I use ksh - but put things like expr in production code so it's maintainable. If you're doing so much string substitution that expr is a performance drain, then the code should be in C or perl, not ksh. I think.

Thanks a lot for your help with this.

Hi,

I need to find the last character of a string. I am using the same code as Perderabo suggested.

I am trying to get the last character 1 in the string mentioned in the code

for this i am trying something like this:

str="fortest_rev_1_1.csv"
strlen=${#str}
strlen1=`expr $strlen - 4`
s=$str|cut -c1-$strlen1
lastchar=${s#${s%?}}
echo $lastchar

I am using bash. The problem is that it does not throw any error neither displays the result. Am i doing anything wrong here.

Help is appreciated

You added a lot of stuff that I didn't have. Your addition does not look legal. I can't guess what it's supposed to do. What are you actually trying to do?

I think using "grep" is much easier to implement. The following bash script will ask you for a string, and add a "/" to the string if the string is not end with a "/".

#!/bin/bash

echo -n "Enter path: "
read p
if [ "$p" = "" ]; then exit; fi
if [ `echo "$p" | grep "[^/]$"` ]; then p="$p/"; fi
echo "$p"