![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|