1825795 Members
2211 Online
109687 Solutions
New Discussion

Strange ksh behaviour

 
Vitek Pepas
Valued Contributor

Strange ksh behaviour

Can anyone explain following ksh behaviour:
(there are no *.cpp files in /, I get the same result on 10.20 and 11.11)

scchp2:/> [[ 1.cpp = '*'.cpp ]] ; echo $?
+ [[ 1.cpp = *.cpp ]]
+ echo 1
1

scchp2:/> a='1.cpp'; e=.cpp ;[[ $a = '*'$e ]]; echo $?
+ a=1.cpp
+ e=.cpp
+ [[ 1.cpp = *.cpp ]]
+ echo 0
0

5 REPLIES 5
TwoProc
Honored Contributor

Re: Strange ksh behaviour

Is there a file called /1 ?
If so, I think that it's your problem...
You're not comparing 1.cpp to "`.cpp" -
you're comparing 1.cpp to possibly "1".cpp.
Meaning - you might be finding true the match between "1.cpp" and "1" b/c you're adding ".cpp" to the find of "*" yourself, instead of asking the OS to match it.

We are the people our parents warned us about --Jimmy Buffett
Rajeev Tyagi
Valued Contributor

Re: Strange ksh behaviour

vitek,

You are not expanding *.cpp to file listing here. With '*' * will be taken as literal character and will be compared with 1.cpp which is not true so you are getting 1 as output.

If you do same comparison without ''

[[ 1.cpp = *.cpp ]] ; echo $?

here * is treated as wild character so * will become 1 in this case and your result will true (0).
Vitek Pepas
Valued Contributor

Re: Strange ksh behaviour

I know I'm comparing strings rather than file listings here. I put the comment about the file to eliminate suspicion that ksh is doing it anyway.
I should get the same result in both cases regardless of files existing in current directory.
TwoProc
Honored Contributor

Re: Strange ksh behaviour

Well, after playing with it for a while I want to a) negate my previous post.... b) say, I've got no idea why it's doing that - except that it has to do with expansion and protection of the test variables - against the expansion of the variables in the other case.
I even thought it had to do with the protection of '*' - but moving it around and unprotecting it yields the same...

/# a='1.cpp'; e=.cpp ;[[ $a = '*'$e ]]; echo $?
0
/# a='1.cpp'; e='*'.cpp ;[[ $a = $e ]]; echo $?
0
/# a='1.cpp'; e=*.cpp ;[[ $a = $e ]]; echo $?
0

Interesting but at last I arrived at...
a='1.cpp'; e=*.cpp ;[[ $a = "$e" ]]; echo $?
1

So, I think it has to with the fact that the shell agrees that 1.cpp is a truth for an expansion of *.cpp; but in the last example the shell knows that the string "1.cpp" is NOT the same as the string literal "*.cpp".

We are the people our parents warned us about --Jimmy Buffett
Vitek Pepas
Valued Contributor

Re: Strange ksh behaviour

I consider this issue to be a bug in ksh.
Perhaps moderator could get HP-UX support's attention regarding the problem.