Comparing if-else and case condition I have a script used for installing few rpms depending on operating system type. Here in this example, the script will install packages on two system "centos" and "suse". The system names are listed in nodes.txt and read as an input while executing the script.

For example:

~]# cat systems.txt

centos
suse


~]# ./myscript.sh systems.txt


There are two scripts written which does the same purpose. One is written using "if-else" and other using "case".

When I use "if-else" condition script, the execution (i.e installation of rpms) completes much faster than using the "case" condition script. Both scripts does the same job, except that the control structure is different. I will have many hundreds of installation criteria listed in the script, and will add/remove new system types within systems.txt file. 

Hence I am not sure which condition is better to choose, and considering my use case. Can someone help me know why does case script taking more time to execute when compared if-else? Any help/recommendations? 

Below the case script which took 10 secs to complete.

#! /bin/bash

while read -d $'\n' -r node
do

# Each node has a unique id assigned and used along with install script
id=$(./name.sh -getid)

case $node in
   centos)
echo $(./install.sh --resourceId=$id --name=httpd)
echo $(./install.sh --resourceId=$id --name=gcc)
      ;;

    suse)
echo $(./install.sh --resourceId=$id --name=compact)
echo $(./install.sh --resourceId=$id --name=glib)
      ;;

   *)
        echo "#No resources found for the node"
    ;;
esac
done &lt; "$1"


Below the if-else script which took 5 secs to complete:

#!/bin/bash
while read -d $'\n' -r node
do

# Each node has a unique id assigned and used along with install script
id=$(./name.sh -getid)

if [ $node == centos ]
then

echo $(./install.sh --resourceId=$id --name=httpd)
echo $(./install.sh --resourceId=$id --name=gcc)

elif [ $node == suse ]
then

echo $(./install.sh --resourceId=$id --name=compact)
echo $(./install.sh --resourceId=$id --name=glib)

else

echo "#No resources found for the node"

fi
done &lt; "$1"
This problem has me perplexed. Frankly, I don't believe you ;-) !!!

There is no logical reason for this code to be so different from each other. There must be something else going on.... For a start, the bash process itself is probably taking almost no time at all, and all the time will be spent running the 'install' sub-processes. I imagine that you are running the install processes twice as many times for some reason.... or that, subsequent installs are faster because you don't need to access the disk/net, or something.

Regardless, I don't believe the difference in performance is because of the case vs. if

As a side note, do you need to execute the installs in a sub-shell and capture the results like you do and then echo them? The install lines should all look similar to:

# Relace this: echo $(./install.sh --resourceId=$id --name=httpd)
# with:
./install.sh --resourceId=$id --name=httpd