Sunday, December 4, 2011

Bash/Shell if conditions are lazy for '&&'(AND) expressions


This script is showing that &&(AND) conditions are optimized in Bash/Shell to not calculate rest of condition if first part is already FALSE.

#!/bin/bash

IS_HOLIDAY='N'
RUN_DAYOFF='N'
#if  [ "x$IS_HOLIDAY" = "xY" ]  && [ "x$RUN_DAYOFF" != "xY" ]; then
if  [ "x$IS_HOLIDAY" = "xY" ]  && [ echo 'sdfs' ]; then
    echo "Skipping unit , today is dayoff"
else
      echo "DO unit , today is dayoff"
fi
echo "finished."

No comments:

Post a Comment