{"id":3413,"date":"2014-08-04T09:42:26","date_gmt":"2014-08-04T01:42:26","guid":{"rendered":"http:\/\/rmohan.com\/?p=3413"},"modified":"2014-08-04T09:42:26","modified_gmt":"2014-08-04T01:42:26","slug":"shell-string-comparison-tests","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3413","title":{"rendered":"shell string comparison tests"},"content":{"rendered":"<p> shell string comparison tests<br \/>\nHere are the operators for performing string comparison tests:<\/p>\n<p>s1          Test if s1 is not the empty string<br \/>\ns1 = s2     Test if s1 equals s2<br \/>\ns1 != s2    Test if s1 is not equal to s2<br \/>\n-n s1       Test if s1 has non-zero size<br \/>\n-z s1       Test if s1 has zero size<br \/>\nHere&#8217;s an example of how to see if two strings are equal:<\/p>\n<p>if [ $foo = $bar ]<br \/>\nthen<br \/>\n  # do something<br \/>\nfi<br \/>\nThis script echoes TRUE:<\/p>\n<p>s1=<\/p>\n<p>if [ -n $s1 ]<br \/>\nthen<br \/>\n  echo &#8220;TRUE&#8221;<br \/>\nelse<br \/>\n  echo &#8220;FALSE&#8221;<br \/>\nfi<br \/>\nThis script echoes FALSE:<\/p>\n<p>s1=bar<\/p>\n<p>if [ -z &#8220;$s1&#8221; ]<br \/>\nthen<br \/>\n  echo &#8220;TRUE&#8221;<br \/>\nelse<br \/>\n  echo &#8220;FALSE&#8221;<br \/>\nfi<\/p>\n<p>Linux shell script math\/number equality tests<br \/>\nHere&#8217;s how you perform math\/number\/arithmetic tests using the Bourne and Bash shells:<\/p>\n<p>n1 -eq n2    Test if n1 equals n2<br \/>\nn1 -ne n2    Test if n1 is not equal to n2<br \/>\nn1 -lt n2    Test if n1 is less than n2<br \/>\nn1 -le n2    Test if n1 is less than or equal to n2<br \/>\nn1 -gt n2    Test if n1 is greater than n2<br \/>\nn1 -ge n2    Test if n1 is greater than or equal to n2<br \/>\nHere&#8217;s an example of how to test whether two numbers are equal:<\/p>\n<p>if [ $n1 -eq $n2 ]<br \/>\nthen<br \/>\n  # do something<br \/>\nfi<br \/>\nLinux shell boolean and\/or\/not operators<br \/>\nThe following boolean and\/or\/not operators can also be used in your tests:<\/p>\n<p>-a    and<br \/>\n-o    or<br \/>\n!     not<br \/>\nHere&#8217;s an example of how to test perform a test using the and operator:<\/p>\n<p>if [ $num -gt 0 -a $num -lt 10 ]<br \/>\nthen<br \/>\n  # do something here<br \/>\nfi<\/p>\n<p>Linux shell script math\/number equality tests<br \/>\nHere&#8217;s how you perform math\/number\/arithmetic tests using the Bourne and Bash shells:<\/p>\n<p>n1 -eq n2    Test if n1 equals n2<br \/>\nn1 -ne n2    Test if n1 is not equal to n2<br \/>\nn1 -lt n2    Test if n1 is less than n2<br \/>\nn1 -le n2    Test if n1 is less than or equal to n2<br \/>\nn1 -gt n2    Test if n1 is greater than n2<br \/>\nn1 -ge n2    Test if n1 is greater than or equal to n2<br \/>\nHere&#8217;s an example of how to test whether two numbers are equal:<\/p>\n<p>if [ $n1 -eq $n2 ]<br \/>\nthen<br \/>\n  # do something<br \/>\nfi<br \/>\nLinux shell boolean and\/or\/not operators<br \/>\nThe following boolean and\/or\/not operators can also be used in your tests:<\/p>\n<p>-a    and<br \/>\n-o    or<br \/>\n!     not<br \/>\nHere&#8217;s an example of how to test perform a test using the and operator:<\/p>\n<p>if [ $num -gt 0 -a $num -lt 10 ]<br \/>\nthen<br \/>\n  # do something here<br \/>\nfi<\/p>\n<p>a=5<br \/>\nb=20<\/p>\n<p>if test \\( $a -gt 0 -a $a -lt 10 \\) -o \\( $b -gt 0 -a $b -lt 20 \\)<br \/>\nthen<br \/>\n   echo &#8220;TRUE&#8221;<br \/>\nelse<br \/>\n  echo &#8220;FALSE&#8221;<br \/>\nfi<br \/>\nThat script echoes &#8220;TRUE&#8221;.<\/p>\n<p>Linux Bourne shell if, then, else, else if (elif) syntax<br \/>\nOne thing that varies from one programming language to another is the if \/ then \/ else \/ else if \/ elseif syntax. In the case of the Bourne shell, the &#8220;else if&#8221; keyword is actually &#8220;elif&#8221;, so a sample Bourne shell if then else if statement looks like this: <\/p>\n<p>if [ -e &#8216;foo&#8217; ]<br \/>\nthen<br \/>\n  echo &#8220;if was true&#8221;<br \/>\nelif [ -e &#8216;bar&#8217; ]<br \/>\nthen<br \/>\n  echo &#8220;elif was true&#8221;<br \/>\nelse<br \/>\n  echo &#8220;came down to else&#8221;<br \/>\nfi<\/p>\n<p>Linux Bourne shell arithmetic<br \/>\nIn the Bourne shell math\/arithmetic is performed using the expr command, like this:<\/p>\n<p>sum=`expr $foo + $bar`<br \/>\nhalf=`expr $foo \/ 2`<br \/>\ntimes=`expr $foo \\* 2`<\/p>\n<p># increment a counter<br \/>\n(( count++ ))<br \/>\nNote that you can&#8217;t have any spaces before or after the equal sign in those (or any) shell script assignment statements.<\/p>\n<p>A few other common Linux shell tricks<br \/>\nHere are a few other tricks\/techniques you will often see in Unix shell scripts:<\/p>\n<p>cmd1 &#038;&#038; cmd2   Run cmd1; if it returns 0 (success), run cmd2<br \/>\ncmd1 || cmd2   Run cmd1; if it returns non-zero, run cmd2<br \/>\ncmd1 &#038; cmd2    Run cmd1 and also cmd2<br \/>\n(ls -1)        Run the command &#8220;ls -1&#8221; in a subshell<\/p>\n","protected":false},"excerpt":{"rendered":"<p> shell string comparison tests Here are the operators for performing string comparison tests:<\/p>\n<p>s1 Test if s1 is not the empty string s1 = s2 Test if s1 equals s2 s1 != s2 Test if s1 is not equal to s2 -n s1 Test if s1 has non-zero size -z s1 Test if s1 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3413"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3413"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3413\/revisions"}],"predecessor-version":[{"id":3414,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3413\/revisions\/3414"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}