{"id":3355,"date":"2014-07-18T12:52:24","date_gmt":"2014-07-18T04:52:24","guid":{"rendered":"http:\/\/rmohan.com\/?p=3355"},"modified":"2014-07-18T15:10:01","modified_gmt":"2014-07-18T07:10:01","slug":"if-statement-and-comparison-operators","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3355","title":{"rendered":"If statement and comparison operators"},"content":{"rendered":"<p>If statement without any brackets:<br \/>\n These are used to check the outcome of a command. It return the exist status of command executed after if.<\/p>\n<p>if grep -i oolala lyrics.txt<br \/>\nthen<br \/>\n  echo &#8220;lyrics.txt file contains oolala&#8221;<br \/>\nelse<br \/>\n  echo &#8220;lyrics.txt file doesn&#8217;t contain oolala&#8221;<br \/>\nfi <\/p>\n<p>if statement with []:<br \/>\nThese are used to check the file types and compare strings.<\/p>\n<p>if [ -z $variable ]<br \/>\nthen<br \/>\n  echo &#8220;Variable is empty&#8221;<br \/>\nelse<br \/>\n  echoo &#8220;Variable is not empty&#8221;<br \/>\nfi<\/p>\n<p>if statement with [[]]:<br \/>\nThese are used for the same purpose as [] but more versatile and doesn\u2019t give error when &#038;&#038;, ||, >, < are used within these brackets unlike [].\n\nif [[ \"$number1\" -eq \"$number2\" ]] \nthen\n  echo \"numbers are equal\"\nelse\n  echo \"numbers are not equal\"\nfi\n\nif statement with no \u201cif\u201d:\nThese are useful to evaluate a condition and take a single action.\n\n[ -f \"$filename\" ] || touch $filename # if $filename doesn't exist, it will touch that file\n[ -f \"$filename\" ] &#038;&#038; rm $filename    # if $filename exists, it will remove that file\n\nnested if statement:\nif [[ \"$fruitname\" == \"apple\" ]]\nthen\n  echo \"Fruit is apple\"\nelif [[ \"$fruitname\" == \"mango\" ]] \nthen\n  echo \"Fruit is mango\"\nelse\n  echo \"Fruit can not be identified\"\nfi\n\nCompound if conditions\nThis is a combination of more than one test expression in if statement. Best explained with an example.\n\nnum1=10\nnum2=20\nif [ \"$num1\" -eq 10 ] &#038;&#038; [ \"$num2\" -eq 20 ]\nthen\n  echo \"Number test successful\"  => It is successful because both numbers match their values<br \/>\nelse<br \/>\n  echo &#8220;Number test failed&#8221;<br \/>\nfi<br \/>\nif [ &#8220;$num1&#8221; -eq 10 ] || [ &#8220;$num2&#8221; -eq 30 ]<br \/>\nthen<br \/>\n  echo &#8220;Number test successful&#8221;  => It is successful because num1 matches 10<br \/>\nelse<br \/>\n  echo &#8220;Number test failed&#8221;<br \/>\nfi<br \/>\nif [ &#8220;$num1&#8221; -eq 10 -a &#8220;$num2&#8221; -eq 20 ]<br \/>\nthen<br \/>\n  echo &#8220;Number test successful&#8221;  => It is successful because both numbers match their values<br \/>\nelse<br \/>\n  echo &#8220;Number test failed&#8221;<br \/>\nfi<br \/>\nif [ &#8220;$num1&#8221; -eq 30 -o &#8220;$num2&#8221; -eq 20 ]<br \/>\nthen<br \/>\n  echo &#8220;Number test successful&#8221;  => It is successful because num2 matches 20<br \/>\nelse<br \/>\n  echo &#8220;Number test failed&#8221;<br \/>\nfi<\/p>\n<p>fruit1=apple<br \/>\nfruit2=mango<br \/>\nif [ &#8220;$fruit1&#8221; == apple ] &#038;&#038; [ &#8220;$fruit2&#8221; == mango ]<br \/>\nthen<br \/>\n  echo &#8220;String test successful&#8221;  => It is successful because both strings match their values<br \/>\nelse<br \/>\n  echo &#8220;String test unsuccessful&#8221;<br \/>\nfi<\/p>\n<p>Test Operators<\/p>\n<p>File test operators:<br \/>\n To be used as:-<br \/>\nif [ &#8220;operator&#8221; $filename ]; then<\/p>\n<p>-e : Filename exists<br \/>\n-d : Filename exists and is a directory<br \/>\n-f : Filename exists and is a regular file<br \/>\n-s : Filename exists and its size is greater than zero<br \/>\n-b : Filename exists and is a block device<br \/>\n-c : Filename exists and is a character device<br \/>\n-p : Filename exists and is a pipe<br \/>\n-L : Filename exists and is a symbolic Link<br \/>\n-S : Filename exists and is a socker<br \/>\n-g : setgid is set on filename<br \/>\n-u : setuid is set on filename<br \/>\n-k : sticky bit is set on filename<br \/>\n-r : user running the script has read permission on given file<br \/>\n-w : user running the script has write permission on given file<br \/>\n-x : user running the script has execute permission on given file<br \/>\n-O : user running the script is owner of given file<br \/>\n-G : user running the script has same group id as of the script<br \/>\nfile1 -nt file2 : File file1 is newer than file2<br \/>\nfile1 -ot file2 : File file1 is older than file2<br \/>\nfile1 -ef file2 : file1 and file2 are hard links to the same file<\/p>\n<p>String operators:<br \/>\n To be used as:-<br \/>\n if [ &#8220;$string1&#8221; <condition> &#8220;$string2&#8221; ]; then<\/p>\n<p>=  : strings are equal<br \/>\n== : strings are equal<br \/>\n!= : strings are not equal<br \/>\n<  : string1 is less than string2 in ASCII order. \n     if [[ \"string1\" < \"string2\" ]] or if [[ \"string1\" \\< \"string2\" ]\n>  : string1 is more than string2 in ASCII order.<br \/>\n     if [[ &#8220;string1&#8221; > &#8220;string2&#8221; ]] or if [[ &#8220;string1&#8221; \\> &#8220;string2&#8221; ]<br \/>\n-z : string is empty<br \/>\n-n : string is not empty<\/p>\n<p>Numeric Operators:<br \/>\n To be used as:-<br \/>\nif [ &#8220;$number1&#8221; <condition> &#8220;$number2&#8221; ]; then<\/p>\n<p>-eq : numbers are equal<br \/>\n-ne : numbers are not equal<br \/>\n-gt : number1 is greater than number2<br \/>\n-ge : number1 is greater than or equal to number2<br \/>\n-lt : number1 is less than number2<br \/>\n-le : number1 is less than or equal to number2<br \/>\n<   : number1 is less than number2. To be used within (()) without the word \"if\". \n      (( \"$num1\" < \"$num2\" ))\n<=  : number1 is less than or equal to number2. To be used within (()) without the word \"if\". \n      (( \"$num1\" <= \"$num2\" ))\n>   : number1 is greater than number2. To be used within (()) without the word &#8220;if&#8221;.<br \/>\n      (( &#8220;$num1&#8221; > &#8220;$num2&#8221; ))<br \/>\n>=  : number1 is greater than or equal to number2. To be used within (()) without the word &#8220;if&#8221;.<br \/>\n      (( &#8220;$num1&#8221; >= &#8220;$num2&#8221; ))<\/p>\n<p>#! \/bin\/ksh<\/p>\n<p>if [[ -s $FILE ]] ; then<br \/>\necho &#8220;$FILE has data.&#8221;<br \/>\nelse<br \/>\necho &#8220;$FILE is empty.&#8221;<br \/>\nfi ;<\/p>\n<p>Reading from a file<\/p>\n<p>for line in $(cat \/etc\/passwd)<br \/>\n do<br \/>\n   echo $line<br \/>\n done<\/p>\n<p>Iterate for a range<\/p>\n<p>for number in {1..10}<br \/>\n do<br \/>\n   echo \u201cCurrent number is $number\u201d<br \/>\ndone<\/p>\n<p>Loop for values given<\/p>\n<p>for planet in earth mars saturn jupiter<br \/>\n do<br \/>\n   echo \u201cThe planet name is $planet\u201d<br \/>\ndone<\/p>\n<p>Loop for variable containing multiple values<\/p>\n<p>planets=\u201dearth mars saturn jupiter pluto\u201d<br \/>\nfor planet in $planets<br \/>\n do<br \/>\n   echo \u201cThe planet name is $planet\u201d<br \/>\ndone<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If statement without any brackets: These are used to check the outcome of a command. It return the exist status of command executed after if.<\/p>\n<p>if grep -i oolala lyrics.txt then echo &#8220;lyrics.txt file contains oolala&#8221; else echo &#8220;lyrics.txt file doesn&#8217;t contain oolala&#8221; fi <\/p>\n<p>if statement with []: These are used to check the [&#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\/3355"}],"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=3355"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3355\/revisions"}],"predecessor-version":[{"id":3358,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3355\/revisions\/3358"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}