Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn shell - if test of substring 3

Status
Not open for further replies.

nelmesrd

Technical User
Jan 15, 2003
2
GB
Does anyone know how to write in a Korn shell script -

if <VARIABLE> ends in <substring> then...

I am trying to get the same result as I would with gre syntax -

VARIABLE | grep -v substring$
 
case $VARIABLE in *$substring) echo OK;; *) echo BAD;; esac

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

Please specify exactly what you want :

VARIABLE="abcdef"
SUBSTRING="def"

or

VARIABLE="def"
SUBSTRING="abcdef"


For this last case, try this

print $SUBSTRING | grep "$VARIABLE$" 1>/dev/null 2>&1
if [ $? -eq 0 ]
then
print "$SUBSTRING ends with $VARIABLE"
else
print "$SUBSTRING does nod end with $VARIABLE"
fi

Ali
 
I believe this will work in the Korn shell...
Code:
VAR=abcdef
STR=def

[[ ${VAR} = @(*${STR}) ]] && print "VAR ends with ${STR}"

# or

if [[ ${VAR} = @(*${STR}) ]]
then
    print "VAR ends with ${STR}"
fi
The construct [tt]@(pattern)[/tt] is used for pattern matching strings in the Korn shell.

I use this in scripts that read a configuration file to skip comment lines. If each comment starts with a pound sign (#), the following will skip them...
Code:
unset IFS

while read LINE
do
    # Skip comments
    [[ ${LINE} = @(#*) ]] && continue
    # Skip blank lines
    [[ -z ${LINE} ]]      && continue

    # Process the line from here...
done < my.conf

Hope this helps.
 
All your answers were helpful. I thought a report would be useful to others with the same question.

PHV - CASE appears to exist to provide multiple 'exits' depending various values of <substring>. As I was only after a YES or NO result I decided not to use this. But prompted by your suggestion I have read up on CASE and better understand it, and no doubt will use it in the future, so thank you for the suggestion.

aau - My problem matches your VARIABLE=abcdef, SUBSTRING=def example. Apologies for not making this clearer. I was successfully able to write my statement along the lines of your example. Thank you for the suggestion.

SamBones - I can confirm that the @(Pattern) construct worked successfully. You confused me a little by calling a hash (#) 'a pound sign' rather than a (£). I presume you must be writing from somewhere abroad of the British Isles?

I ended adopting the following simple syntax that I discovered worked -

If [[ $<VARIABLE> != *<substring> ]]

Where the asterisk (*) represents all characters at the beginning of the string being tested, and <substring> represents the characters at the end.

...unless I have misunderstood of course?
 
(_8^(o) D'oh!

Sorry about the 'pound sign'/hash confusion! You do assume correctly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top