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!

Korn string compair 1

Status
Not open for further replies.

awood69

Programmer
Feb 27, 2012
19
US
I am new to Korn program and trying to do a simple task.


$group_type = "PLT01"
if [[ $group_type == PLT* ]]
then
print "Success"
fi

Not sure why this will not work?
 
Hi
[ul]
[li]Do not use sigil on the left hand side of the assignment.[/li]
[li]Do not leave spaces around the equality sign ( = ) in the assignment.[/li]
[/ul]
Code:
[gray]# wrong[/gray]
[navy]$group_type[/navy] [teal]=[/teal] [green][i]"PLT01"[/i][/green]

[gray]# correct[/gray]
group_type[teal]=[/teal][green][i]"PLT01"[/i][/green]

Feherke.
 


1) You cannot have spaces befor/after the equal sing in 'group_type = "PLT01"'

2) The "==" in the "if" statement does not recognize regular expressions.

Try:
Code:
group_type="PLT01"
[ `echo $group_type|grep "^PLT"|wc -l` -gt 0 ] && print "Success"
[/code
[3eyes] 

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

LKBrwnDBA said:
The "==" in the "if" statement does not recognize regular expressions.
Correct. But I think neither the OP intended that, it looks more like pattern, which is Ok :
man ksh88 said:
[tt]string = pattern
True, if string matches pattern.[/tt]
man ksh93 said:
[tt]string = pattern
Same as == above, but is obsolete.[/tt]


Feherke.
 

Thanks Feherke.

Unfortunately all I have to test is ksh (AIX) and Redhat 6 (bash) and on neither of these the "pattern" works.
[noevil]






----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

Interesting. I tested only with [tt]mksh[/tt] ( MirBSD Korn shell ), but I quoted the official man pages from kornshell.com to be sure I am talking about "official" features.

By the way, you used [tt][[[/tt] ... [tt]]][/tt], right ?

Feherke.
 

Ooops....

It does work with the [[...]]
[thumbsup2]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Ksh has a nice pattern match operator that is pretty forgiving of bad input. It's [tt]@(pattern)[/tt].
Code:
$group_type = "PLT01"

if [[ $group_type == [b]@(PLT*)[/b] ]]
then
    print "Success"
fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top