Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Testing the last character in a string

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-07-2004
dbrundrett dbrundrett is offline
Registered User
 
Join Date: Jan 2002
Last Activity: 9 May 2005, 8:52 AM EDT
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 07-07-2004
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 448 Times in 264 Posts
In ksh:
$ a=abcdefg
$ lastchr=${a#${a%?}}
$ echo $lastchr
g
$
Sponsored Links
    #3  
Old Unix and Linux 07-07-2004
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 448 Times in 264 Posts
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"/
    #4  
Old Unix and Linux 07-07-2004
dbrundrett dbrundrett is offline
Registered User
 
Join Date: Jan 2002
Last Activity: 9 May 2005, 8:52 AM EDT
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Appreciate your help
Sponsored Links
    #5  
Old Unix and Linux 07-07-2004
dbrundrett dbrundrett is offline
Registered User
 
Join Date: Jan 2002
Last Activity: 9 May 2005, 8:52 AM EDT
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old Unix and Linux 07-07-2004
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 4 July 2015, 9:25 PM EDT
Location: NM
Posts: 10,496
Thanks: 344
Thanked 869 Times in 807 Posts
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.


Code:
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.
Sponsored Links
    #7  
Old Unix and Linux 07-07-2004
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 448 Times in 264 Posts
Quote:
Originally posted by dbrundrett
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
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.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Testing the length of a string intelinside Shell Programming and Scripting 1 04-15-2012 10:55 AM
Testing for non-zero length string longjon UNIX for Dummies Questions & Answers 5 12-08-2011 06:14 PM
testing the last character Nicol Shell Programming and Scripting 3 06-04-2009 08:54 AM
Korn: How to loop through a string character by character shew01 Shell Programming and Scripting 10 12-02-2008 06:58 AM
bash: testing if string is a number eur0dad Shell Programming and Scripting 3 07-19-2006 11:50 PM



All times are GMT -4. The time now is 10:42 PM.