Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!helios!bcm!dimacs.rutgers.edu!rutgers!modus!gear!am!alex From: alex@am.sublink.org (Alex Martelli) Newsgroups: comp.unix.shell Subject: Re: string comparisons Message-ID: <1991Mar19.002033.1305@am.sublink.org> Date: 19 Mar 91 00:20:33 GMT References: <2847@kluge.fiu.edu> <1991Mar14.225901.3973@mrspoc.Transact.COM> <1991Mar17.061632.4096@cbnewsh.att.com> Organization: Premiata Famiglia Martelli & Figli Lines: 94 wcs@cbnewsh.att.com (Bill Stewart 908-949-0705 erebus.att.com!wcs) writes: :In article <1991Mar14.225901.3973@mrspoc.Transact.COM> steven@Transact.COM writes: :]acmfiu@serss0.fiu.edu (ACMFIU) writes: :]>under csh or sh, how do i compare two strings? :]Either use expr(1) or awk(1). Depends on how elaborate you want to get. :The test program (aka "[") is accessible from either shell, and is a :builtin for modern shells like ksh. In sh, just do : if [ "$foo" = "$bar" ] : then true-stuff : else false-stuff I believe that the following approach should be often faster: case $foo in "$bar") true-stuff ;; *) false-stuff ;; esac since "case", and the "globbing-like" behavior of its pattern-matching, IS guaranteed to be builtin to sh. NOT earth-shattering differences, mind you!, but given these two scripts on my 386/20, Interactive 2.2: ---cut: pep #!/bin/sh fai='abe*cou' ogu='abe*cou' for i in 0 1 2 3 4 5 6 7 8 9 do case $fai in "$ogu") echo "$fai" si! ;; *) echo "$fai" no! ;; esac >/dev/null done fai='abetocou' for i in 0 1 2 3 4 5 6 7 8 9 do case $fai in "$ogu") echo "$fai" si! ;; *) echo "$fai" no! ;; esac >/dev/null done ---cut: pap #!/bin/sh fai='abe*cou' ogu='abe*cou' for i in 0 1 2 3 4 5 6 7 8 9 do if [ $fai = $ogu ] then echo "$fai" si! ; else echo "$fai" no! ; fi >/dev/null done fai='abetocou' for i in 0 1 2 3 4 5 6 7 8 9 do if [ $fai = $ogu ] then echo "$fai" si! ; else echo "$fai" no! ; fi >/dev/null done ---end cut /bin/time says, for pap: real 1.6 user 0.3 sys 1.2 while for pep: real 0.5 user 0.0 sys 0.4 with very repeatable measurements (+-0.1 second max on each number over quite a few repetitions), so the case would appear to be 2 to 3 times faster than the if [ ] (despite the "dilution" of the for, echos to /dev/null, and other trinkets in common between the two scripts). Besides, the globbing in the case statement turns out handy quite often... (sometimes it's a bother instead - see the above quotes around "$ogu"). I learned Unix on underpowered/overloaded machines, so my istincts push me to "optimize" even such trifles. I realize that "if [ ]" will look more readable/understandable/maintainable than "case" to most people, and no doubt it may even be faster in shells with test built-in... so please don't flame on these accounts! I'm just trying to suggest an alternative technique that may avoid some extra forks in more "traditional" sh's. -- Alex Martelli - (home snailmail:) v. Barontini 27, 40138 Bologna, ITALIA Email: (work:) martelli@cadlab.sublink.org, (home:) alex@am.sublink.org Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; Fax: ++39 (51) 366964 (work only), Fidonet: 332/401.3 (home only).