{"id":3420,"date":"2014-08-06T00:48:50","date_gmt":"2014-08-05T16:48:50","guid":{"rendered":"http:\/\/rmohan.com\/?p=3420"},"modified":"2014-08-07T14:08:57","modified_gmt":"2014-08-07T06:08:57","slug":"if-condition-integer-expression-expected","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3420","title":{"rendered":"if condition-integer expression expected"},"content":{"rendered":"<p><strong>if condition-integer expression expected<\/strong><\/p>\n<p>ADV1=94.3<br \/>\nQuantity=96.3<br \/>\nif [ $Quantity -eq $ADV1 ]; then<br \/>\n    echo &#8220;quantity is greter&#8221;<br \/>\nfi<\/p>\n<p>echo &#8220;&#8221; | nawk -v ADV1=94.3 -v Quantity=96.3 &#8216;{if(ADV1<Quantity){print \"Quantity is greater\"}}'\nQuantity is greater\n\n\n\nADV1=99.3\nQuantity=96.6\necho \"adv1: $ADV1\"\necho \"Quantity : $Quantity\"\nif [ $(echo \"$ADV1 > $Quantity &#8220;|bc) -eq 1 ] ; then<br \/>\necho &#8220;$ADV1 is greter&#8221;<br \/>\nelse<br \/>\necho &#8220;$Quantity is greater&#8221;<br \/>\nfi<\/p>\n<p>ADV1=94.3<br \/>\nQuantity=96.3<br \/>\nif [ $Quantity -gt $ADV1 ]; then<br \/>\n    echo &#8220;quantity is greter&#8221;<br \/>\nfi<\/p>\n<p>if [ &#8220;$var1&#8221; -lt &#8220;$var2&#8221; ]; then<br \/>\n    echo &#8220;$var1 is lt $var2&#8221;<br \/>\nelse<br \/>\n    echo &#8220;$var2 is lt $var1&#8221;<br \/>\nfi<\/p>\n<p>if ((var1<var2)); then\n    echo \"$var1 is lt $var2\"\nelse\n    echo \"$var2 is lt $var1\"\nfi\n\n\n\nif [[ $var1 -lt $var2 ]]; then\n    echo \"$var1 is lt $var2\"\nelse\n    echo \"$var2 is lt $var1\"\nfi\n\n\n\nact=\"add\"\nif [[ $act = \"add\" ]]\nthen\n    echo good\nelse\n    echo not good\nfi\n\n\n#!\/bin\/bash\nif [ $# -ne 1 ];\nthen\n  echo \"\/root\/script.sh a|b\"\nelif [ $1 ='a' ];\nthen\n  echo \"b\"\nelif [ $1 ='b' ]; then\n  echo \"a\"\nelse \n  echo \"\/root\/script.sh a|b\"\nfi\n\n\n\n\n\n\n\n\n\n\n\nIn bash this is easy, just tie them together with &#038;&#038;:\n\ncommand1 &#038;&#038; command2 &#038;&#038; command3\nYou can also use the nested if construct:\n\nif command1\n   then\n       if command2\n           then\n               do_something\n           else\n               exit\n       fi\n   else\n       exit\nfi\n\n[ $? -eq 0 ] || exit $?; # exit for none-zero return code\n\n\n#\/bin\/bash\n\n#pgm to monitor\ntail -f \/var\/log\/messages >> \/tmp\/log&#038;<br \/>\n# background cmd pid<br \/>\npid=$!<br \/>\n# loop to monitor running background cmd<br \/>\nwhile :<br \/>\ndo<br \/>\n    ps ax | grep $pid | grep -v grep<br \/>\n    ret=$?<br \/>\n    if test &#8220;$ret&#8221; != &#8220;0&#8221;<br \/>\n    then<br \/>\n        echo &#8220;Monitored pid ended&#8221;<br \/>\n        exit<br \/>\n    fi<br \/>\n    sleep 5<\/p>\n<p>done<\/p>\n<p>wait $pid<br \/>\necho $?<\/p>\n<p>!\/bin\/bash<br \/>\nCHECK=$0<br \/>\nSERVICE=$1<br \/>\nDATE=`date`<br \/>\nOUTPUT=$(ps aux | grep -v grep | grep -v $CHECK |grep $1)<br \/>\necho $OUTPUT<br \/>\nif [ &#8220;${#OUTPUT}&#8221; -gt 0 ] ;<br \/>\nthen echo &#8220;$DATE: $SERVICE service running, everything is fine&#8221;<br \/>\nelse echo &#8220;$DATE: $SERVICE is not running&#8221;<br \/>\nfi<\/p>\n<p>#!\/bin\/sh<\/p>\n<p>SERVICE=&#8221;$1&#8243;<br \/>\nRESULT=`ps -a | sed -n \/${SERVICE}\/p`<\/p>\n<p>if [ &#8220;${RESULT:-null}&#8221; = null ]; then<br \/>\n    echo &#8220;not running&#8221;<br \/>\nelse<br \/>\n    echo &#8220;running&#8221;<br \/>\nfi<\/p>\n<p>#!\/bin\/bash<br \/>\nps axho comm| grep $1 > \/dev\/null<br \/>\nresult=$?<br \/>\necho &#8220;exit code: ${result}&#8221;<br \/>\nif [ &#8220;${result}&#8221; -eq &#8220;0&#8221; ] ; then<br \/>\necho &#8220;`date`: $SERVICE service running, everything is fine&#8221;<br \/>\nelse<br \/>\necho &#8220;`date`: $SERVICE is not running&#8221;<br \/>\n\/etc\/init.d\/$1 restart<br \/>\nfi<\/p>\n<p>#!\/bin\/sh<br \/>\nSERVICE=$1<br \/>\nif ps ax | grep -v grep | grep -v $0 | grep $SERVICE > \/dev\/null<br \/>\nthen<br \/>\n    echo &#8220;$SERVICE service running, everything is fine&#8221;<br \/>\nelse<br \/>\n    echo &#8220;$SERVICE is not running&#8221;<br \/>\nfi<\/p>\n<p>!\/bin\/sh<\/p>\n<p>PROCESS=&#8221;$1&#8243;<br \/>\nPROCANDARGS=$*<\/p>\n<p>while :<br \/>\ndo<br \/>\n    RESULT=`pgrep ${PROCESS}`<\/p>\n<p>    if [ &#8220;${RESULT:-null}&#8221; = null ]; then<br \/>\n            echo &#8220;${PROCESS} not running, starting &#8220;$PROCANDARGS<br \/>\n            $PROCANDARGS &#038;<br \/>\n    else<br \/>\n            echo &#8220;running&#8221;<br \/>\n    fi<br \/>\n    sleep 10<br \/>\ndone    <\/p>\n<p>#!\/bin\/bash<br \/>\nps_out=`ps -ef | grep $1 | grep -v &#8216;grep&#8217; | grep -v $0`<br \/>\nresult=$(echo $ps_out | grep &#8220;$1&#8221;)<br \/>\nif [[ &#8220;$result&#8221; != &#8220;&#8221; ]];then<br \/>\n    echo &#8220;Running&#8221;<br \/>\nelse<br \/>\n    echo &#8220;Not Running&#8221;<br \/>\nfi<\/p>\n<p># simulate a long process that will have an identifiable exit code<br \/>\n(sleep 15 ; \/bin\/false) &#038;<br \/>\nmy_pid=$!<\/p>\n<p>while   ps | grep &#8221; $my_pid &#8221;     # might also need  | grep -v grep  here<br \/>\ndo<br \/>\n    echo $my_pid is still in the ps output. Must still be running.<br \/>\n    sleep 3<br \/>\ndone<\/p>\n<p>echo Oh, it looks like the process is done.<br \/>\nwait $my_pid<br \/>\nmy_status=$?<br \/>\necho The exit status of the process was $my_status<\/p>\n<p>#!\/bin\/sh<\/p>\n<p>cmd() { sleep 5; exit 24; }<\/p>\n<p>cmd &#038;   # Run the long running process<br \/>\npid=$!  # Record the pid<\/p>\n<p># Spawn a process that coninually reports that the command is still running<br \/>\nwhile echo &#8220;$(date): $pid is still running&#8221;; do sleep 1; done &#038;<br \/>\nechoer=$!<\/p>\n<p># Set a trap to kill the reporter when the process finishes<br \/>\ntrap &#8216;kill $echoer&#8217; 0<\/p>\n<p># Wait for the process to finish<br \/>\nif wait $pid; then<br \/>\n    echo &#8220;cmd succeeded&#8221;<br \/>\nelse<br \/>\n    echo &#8220;cmd FAILED!! (returned $?)&#8221;<br \/>\nfi<\/p>\n<p>#!\/bin\/bash<br \/>\n&#8230;.<br \/>\ndoSomething &#038;<br \/>\nlocal pid=$!<br \/>\nwhile [ -d \/proc\/$pid ]; do # While directory exists, the process is running<br \/>\n    doSomethingElse<br \/>\n    &#8230;.<br \/>\nelse # when directory is removed from \/proc, process has ended<br \/>\n    wait $pid<br \/>\n    local exit_status=$?<br \/>\ndone<\/p>\n<p>#!\/bin\/sh<br \/>\ncd \/usr\/src\/linux<br \/>\nif [ &#8220;$?&#8221; -eq &#8220;0&#8221; ]; then<br \/>\n  make dep<br \/>\n    if [ &#8220;$?&#8221; -eq &#8220;0&#8221; ]; then<br \/>\n      make bzImage<br \/>\n      if [ &#8220;$?&#8221; -eq &#8220;0&#8221; ]; then<br \/>\n        make modules<br \/>\n        if [ &#8220;$?&#8221; -eq &#8220;0&#8221; ]; then<br \/>\n          make modules_install<br \/>\n          if [ &#8220;$?&#8221; -eq &#8220;0&#8221; ]; then<br \/>\n            cp arch\/i386\/boot\/bzImage \/boot\/my-new-kernel<br \/>\n            if [ &#8220;$?&#8221; -eq &#8220;0&#8221; ]; then<br \/>\n              cp System.map \/boot\/<br \/>\n              if [ &#8220;$?&#8221; -eq &#8220;0&#8221; ]; then<br \/>\n                echo &#8220;Your new kernel awaits, m&#8217;lord.&#8221;<br \/>\n              fi<br \/>\n            fi<br \/>\n          fi<br \/>\n        fi<br \/>\n      fi<br \/>\n    fi<br \/>\n  fi<br \/>\nfi<\/p>\n","protected":false},"excerpt":{"rendered":"<p>if condition-integer expression expected<\/p>\n<p>ADV1=94.3 Quantity=96.3 if [ $Quantity -eq $ADV1 ]; then echo &#8220;quantity is greter&#8221; fi<\/p>\n<p>echo &#8220;&#8221; | nawk -v ADV1=94.3 -v Quantity=96.3 [&#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\/3420"}],"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=3420"}],"version-history":[{"count":6,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3420\/revisions"}],"predecessor-version":[{"id":3437,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3420\/revisions\/3437"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}