Search code examples
bashshellif-statementscriptinglogical-operators

IF statement vs logical operator not working as expected


I am writing a simple function to check on three different files existence in which it should return "yes" if ALL three files exist and "Partially" if one or more file exists and "no" if none of the three exist.

The function is called IsPortalDown and it has a case switch; NodeOne is the one in use at this point.

Here's my function

function IsPortalDown() {
  case "$1" in
    NodeOne )
    if [[ -f $wpsdmgr ]] && [[ -f $wpsnodeagent ]] && [[ ! -f $wpsserver ]];
    then
      echo "Yes"
    elif [[ ! -f $wpsdmgr ]] || [[ ! -f $wpsnodeagent ]] || [[ ! -f $wpsserver ]];
    then
      echo "Partially"
    else
      echo "No"
    fi;
      ;;
    NodeTwo )
      ;;
  esac
}

A few constants are defined to clean things up a little bit. See below

PortalProfileLogsDir=/opt/IBM/WebSphere/wp_profile/logs
WPSDMGRProfileLogsDir=/opt/IBM/WebSphere/AppServer/profiles/dmgr01/logs
wpsdmgr=$WPSDMGRProfileLogsDir/dmgr/dmgr.pid
wpsnodeagent=$PortalProfileLogsDir/nodeagent/nodeagent.pid
wpsserver=$PortalProfileLogsDir/WebSphere_Portal/WebSphere_Portal.pid

Here's how I call the function and expected result below.

IsPortalDown NodeOne

If all three files exist the output shall be as below

[wpssrvr@tstdydalbpt0101 dmgr]$ IsPortalDown NodeOne 
Yes

If one of the three files exist the output shall be as below

[wpssrvr@tstdydalbpt0101 dmgr]$ IsPortalDown NodeOne 
Partially

If none of the three files exist the output shall be as below

[wpssrvr@tstdydalbpt0101 dmgr]$ IsPortalDown NodeOne 
No

My issue now is that if one of the three files or all three files does not exist I am constantly getting Yes instead of Partially.

Any help is appreciated. Thanks


Solution

  • This might simplify the logic somewhat: count the number of files that exist:

    nfiles=0
    for file in "$wpsdmgr" "$wpsnodeagent" "$wpsserver"; do
        [[ -f $file ]] && ((++nfiles))
    done
    
    case $nfiles in
        3) echo Yes ;;
        0) echo No ;;
        *) echo Partially ;;
    esac