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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh: check if varaible's value ends with specific string

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

here are my tries:

$ a=abc
$ [[ "$a" = @(abc) ]] && echo ends with abc
ends with abc
$ a=aabc
$ [[ "$a" = @(abc) ]] && echo ends with abc
$ [[ "$a" = @(abc)$ ]] && echo ends with abc
$

 
Hi

Code:
[[ "$a" = [highlight]*[/highlight]@(abc) ]] && echo ends with abc

[gray]# or just[/gray]

[[ "$a" = [highlight]*[/highlight]abc ]] && echo ends with abc
I think you misunderstood the meaning of [tt]@(...)[/tt] sub-patterns. They are meaningful when they enclose a pattern-list ( as man calls them ), so something that describes multiple alternatives, like [tt]@(foo|bar)[/tt] or [tt]@([aeiou])[/tt].


Feherke.
feherke.ga
 
As feherke says, the KSH '@()' is supposed to fully contain the pattern or pattern-list. So it should look like this (asterisk inside the parens)...

Code:
[[ "$a" = @(*abc) ]] && print "Ends with 'abc'"

As he mentions, with pattern as simple as this, it's not really needed, but with more complicated patterns, the whole pattern should be in the parens.

Putting the whole pattern in the parens also lets you include spaces in your pattern...

Code:
STR="1 2 3 4 5"
[[ $STR = @(* 2 3 *) ]] && print "Contains ' 2 3 '"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top