|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
Comparing strings with the shell
From test(1):
Code:
s1 = s2 True if the strings s1 and s2 are identical. s1 != s2 True if the strings s1 and s2 are not identical. s1 < s2 True if string s1 comes before s2 based on the ASCII value of their characters. s1 > s2 True if string s1 comes after s2 based on the ASCII value of their characters. Code:
$ if [ 'aaa' < 'aab' ] ; then echo stringwise smaller ; fi ksh: cannot open aab: No such file or directory Code:
$ if [ 'aaa' \< 'aab' ] ; then echo stringwise smaller ; fi ksh: [: <: unexpected operator/operand Then I remember that shells have built-in's commands that sometimes behave differently than the ones residing in /bin/ for example. Checking the shell test command From ksh(1): Code:
test expression [ expression ] test evaluates the expression and returns zero status if true, 1 if false, or greater than 1 if there was an error. It is normally used as the condition command of if and while statements. [snip] -n string string is not empty. -z string string is empty. string = string Strings are equal. string == string Strings are equal. string != string Strings are not equal number -eq number Numbers compare equal. The /bin/test executable instead of the ksh one works with escaping the '<': Code:
$ if /bin/test "aap" \< "aap2" ; then echo Smaller! ; fi Smaller! Code:
$ if /bin/test "aap2" > "aap" ; then echo Larger! ; fi Larger! $ if /bin/test "aap" > "aap" ; then echo Larger! ; fi Larger! $ ls -l aap -rw-r--r-- 1 adriaan adriaan 0 Dec 31 02:05 aap Code:
$ if /bin/test "aap" \> "aap" ; then echo Larger! ; fi $
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
Tags |
/bin/test, /bin/[, shell string compare, string compare |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
which shell is better? | ibara | OpenBSD General | 7 | 14th March 2014 03:00 PM |
OpenBSD: Comparing Errata and -Stable | jggimi | Guides | 4 | 6th June 2012 06:28 AM |
ask for a shell script | Simon | Programming | 5 | 27th April 2010 01:07 AM |
Shell Scripting with BSD | chavez243 | Programming | 9 | 3rd December 2008 03:03 AM |
Color shell? | giga | FreeBSD General | 3 | 14th August 2008 12:07 AM |