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's [[/]] and wildcard expansion 1

Status
Not open for further replies.

kornShellScripter

Programmer
Apr 27, 2007
24
GB
I'm testing for the existence of files that begin with, say, "gene" and have 8 characters in the filename.

So gene????

This will not work:

Code:
if [[ -f gene???? ]]

because [[/]] does not allow wildcard expansion.

So I'm currently using

Code:
if [ -f gene???? ]

But it would be nice to keep this test inside the korn shell instead of having to use an external command ([/] in this case)

Is this possible?
 
Hi

kornShellScripter said:
because [[/]] does not allow wildcard expansion.
That is because [tt][[ ]][/tt] tries to do regular expression matching.
kornShellScripter said:
it would be nice to keep this test inside the korn shell instead of having to use an external command
YMMV, but...
Code:
[blue]master #[/blue] type [
[ is a shell builtin

[blue]master #[/blue] type test
test is a shell builtin

[blue]master #[/blue] echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2

Feherke.
 
If you insist on [[ ... ]]
if [[ -n $(ls gene???? 2>/dev/null) ]]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
[tt]# type [[
[[ is a reserved shell keyword
# type [
[ is a shell builtin
# type test
test is a shell builtin[/tt]

So if it's all internal, what, really, is the difference between a "reserved shell keyword" and a "shell builtin" and does it really matter for efficiency purposes?
 
My understanding:
A "reserved shell keyword" is part of the language (like "while", "if", "case", ...)
A "shell builtin" is a function or command compiled with the shell.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

kornShellScripter said:
what, really, is the difference between a "reserved shell keyword" and a "shell builtin"
I can show you only a quick [tt]bash[/tt] example :
Bash:
[gray]# builtin is not reserved[/gray]
[blue]master #[/blue] function [() { echo ">>$1<<"; }
[blue]master #[/blue] [ lol
>>lol<<

[gray]# reserved keyword is... somehow reserved[/gray]
[blue]master #[/blue] function [[() { echo ">>$<<"; }
[blue]master #[/blue] [[ lol
bash: unexpected token `newline', conditional binary operator expected
bash: syntax error near `lol'
kornShellScripter said:
does it really matter for efficiency purposes?
You may test it yourself.
Code:
[blue]master #[/blue] i=0; time while [[ $i -lt 100000 ]]; do ((i++)); done
    1.87s real     1.86s user     0.00s system

[blue]master #[/blue] i=0; time while [ $i -lt 100000 ]; do ((i++)); done
    2.35s real     2.23s user     0.00s system
More spectacular with [tt]bash[/tt] :
Bash:
[blue]master #[/blue] i=0; time while [[ $i -lt 100000 ]]; do ((i++)); done

real    0m20.441s
user    0m8.938s
sys     0m8.406s

[blue]master #[/blue] i=0; time while [ $i -lt 100000 ]; do ((i++)); done

real    0m27.347s
user    0m11.953s
sys     0m7.938s

Feherke.
 

Hi, the [tt]((i++))[/tt] doesn't appear to work on my ksh:

Code:
[blue]master #[/blue] i=0; time while [[ $i -lt 100000 ]]; do ((i++)); done
ksh: i++: more tokens expected
[blue]master #[/blue] ps -fp$$
     UID   PID  PPID  C    STIME TTY      TIME CMD
master   12211 12210  0 11:24:27 pts/18   0:00 -ksh
 
Hi

Hmm... Maybe you have an old [tt]ksh[/tt]. My example was tested with (pd)[tt]ksh[/tt], but as far as I know, it works with [tt]ksh[/tt] 93 too.

As the [tt](( ))[/tt] evaluation was present in [tt]ksh[/tt] 88 too, try to just change the syntax. Maybe only the post-increment operator is not supported.
Code:
((i+=1))

[gray]# or[/gray]

((i=i+1))

Feherke.
 
yeah that worked. Thanks :)

Code:
> i=0;time while [[ $i -lt 100000 ]]; do ((i+=1));done

real    0m1.44s
user    0m1.44s
sys     0m0.00s

> i=0;time while [ $i -lt 100000 ]; do ((i+=1));done

real    0m1.95s
user    0m1.95s
sys     0m0.00s
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top