The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read in a file character by character - replace any unknown ASCII characters with spa raghav525 Shell Programming and Scripting 1 04-20-2009 02:52 PM
Delete between 10th character and 20th character nuno_fbo UNIX for Dummies Questions & Answers 2 12-17-2008 03:08 PM
check for a particular character inside a file and substitute with a given character? karthikprasathk AIX 1 07-01-2008 04:29 AM
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 04:18 PM
Testing the last character in a string dbrundrett Shell Programming and Scripting 11 07-15-2004 01:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-04-2009
Nicol Nicol is offline
Registered User
  
 

Join Date: Sep 2003
Location: Paris
Posts: 128
testing the last character

hi

i try to test the last character in a variable (here $i )

assume i=kljlkjlkA it should be KO and lkjljjlT KO

if [ "echo $i|grep A$" ]
then
echo "ending with A"
else
echo "no A at the end"
fi

whether i is ending or not with A i got "no A at the end"

i tried with simple [ double this is the same , i think the pb is with the Interpretation of A$

thanks in advance
Christian
  #2 (permalink)  
Old 06-04-2009
dr.house dr.house is offline
Registered User
  
 

Join Date: Dec 2008
Location: Switzerland
Posts: 229
It's not that simple, I daresay - but possible, of course:


Code:
#! /bin/bash

INPUT="$1"
LENGTH=${#INPUT}
if [ "${INPUT:$(($LENGTH-1)):1}" = "O" ]
then
  echo "good"
else
  echo "bad"
fi

exit 0


Code:
[house@leonov] sh test.bash 'kljlkjlkA it should be KO and lkjljjlT KO'
good

  #3 (permalink)  
Old 06-04-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,938
Here are two ways of doing it. Both ways work in bash and ksh93

Code:
i=kljlkjlkA

if [[ ${i: -1} == "A" ]]
then
    echo "ending with A"
else
    echo "no A at the end"
fi

if [[ $i =~ A$ ]]
then
    echo "ending with A"
else
    echo "no A at the end"
fi

  #4 (permalink)  
Old 06-04-2009
Nicol Nicol is offline
Registered User
  
 

Join Date: Sep 2003
Location: Paris
Posts: 128
thanks but it doesn't work for me :

# sh test.bash 'kljlkjlkA it should be KO and lkjljjlT KO'
test.bash[6]: "${INPUT:$(($LENGTH-1)):1}": bad substitution

i'm in ksh but in bash it's the same !

regards
Christian

-----Post Update-----

Thanks to help me :

i=kljlkjlkA

if [[ ${i: -1} == "A" ]]
then
echo "ending with A"
else
echo "no A at the end"
fi

=> i got :

# test.ksh
test.ksh: ${i: -1}: bad substitution

if [[ $i =~ A$ ]]
then
echo "ending with A"
else
echo "no A at the end"
fi

==> i got :
test.ksh
test.ksh: syntax error at line 1 : `=~' unexpected

regards
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0