Optimize and Speedup the script

Hi All,

There is a script (test.sh) which is taking more CPU usage. I am attaching the script in this thread.

Could anybody please help me out to optimize the script in a better way.

Thanks,
Gobinath

I tried to get this script as much as can, but since the logfile is also somehow contains more info, it's started utilize the CPU more. Anybody thru some light to resolve this issue to reduce the overburden the CPU.

Here is a script:

#!/bin/sh

MON_NAME=$1
pcmon="/tmp/pcmon"
CMD="cat"
OPTION="-option"
DIR=$2 # complete path of directory [Absolute Path]
FILE_PAT=$3 # File Pattern
value=`date |cut -c12-13`
print "$value"
extension=`date |cut -c15-16`
print "$extension"
SEARCH_NAME=$DIR/$FILE_PAT
STATUS=0

CMD_OUTPUT=`cat $SEARCH_NAME | grep -i "$4" | wc -l`
echo "$CMD_OUTPUT"
OPTION_APP_NAME="PROCNAME=$CMD_OUTPUT"
case $4 in  "Too many open files")  
STATUS=0       
       if [ $value -eq 11 ] || [ $value -eq 15 ]
        then 
              if [ $CMD_OUTPUT -gt 500 ]
                then
                   if [ $extension -lt 12 ]
                     then
                       STATUS=1 
                   fi
              fi
        fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
        ;;
        "ERR_REASON:255")   
        if [ $CMD_OUTPUT -gt 500 ]
        then
                STATUS=1
        fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
       ;;
       "ERR_REASON:20")
        if [ $CMD_OUTPUT -gt 500 ]
        then
                STATUS=1
        fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
       ;;
        "ERR_REASON:8#")  
        if [ $CMD_OUTPUT -gt 10 ]
        then
                STATUS=1
        fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
            ;;
         "ERR_REASON:88")
        if [ $CMD_OUTPUT -gt 100 ]
        then
                STATUS=1
        fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
           ;;
              "ERR_REASON:10")
           STATUS=0 
         if [ $value -eq 23 ] && [ $extension -lt 55 ]
         then
                       if [ $CMD_OUTPUT -gt 1 ]
                          then
                             STATUS=1
                      fi
        fi 
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
       
                  ;;
            "ERR_REASON:11")
              STATUS=0 
         if [ $value -eq 23 ] && [ $extension -lt 55 ]
         then
                      if [ $CMD_OUTPUT -gt 1 ]
                          then
                              STATUS=1
                       fi
           fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
           ;;
          "ERR_REASON:2")
           STATUS=0
        if [ $value -eq 23 ] && [ $extension -lt 55 ]
         then
                   if [ $CMD_OUTPUT -gt 1 ]
                   then
                       STATUS=1
                    fi
        fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
              ;;
           "ERR_REASON:3")
       STATUS=0 
        if [ $value -eq 23 ] && [ $extension -lt 55 ]
         then
                    if [ $CMD_OUTPUT -gt 1 ]
                    then
                          STATUS=1
                    fi
       fi
        echo "$pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME"
        $pcmon $MON_NAME=$STATUS $OPTION $OPTION_APP_NAME
esac
exit;

Anybody pls help me on this script....

Oddities picked up visually.

This block of the case statement has no terminating ";;" .
"ERR_REASON:3")
A line should never end with a semi-colon.
exit;
should be
exit
What is in this file which you are trying to execute in several places in the script?
pcmon="/tmp/pcmon"

Without reading the code very much, I noticed you have a whole bunch of if/then statements operating on a single variable: probably a better choice would be to use a switch/case operator.

@kodak
The script is mostly a case statement but is badlly laid out such that this is not easy to follow.

case $4 in  "Too many open files")  

thru

esac 

Thank you very much methyl, kodak.

pcmon is a script that will send information to monitoring system.

Could you please clarify somne of dobuts:

  1. What happen if I not close case statement?

  2. How could I avoid so many if then loops and make my script more optimize?

  3. As you said "The script is mostly a case statement but is badlly laid out ", pls let me know can make this one is as good as compared to the old one?

Thanks,
Gobinathan.S

---------- Post updated at 09:21 PM ---------- Previous update was at 09:09 PM ----------

Also, Is it possible to use the statement as below or suggest me the way to go in a good way

Instead of using like:

case $4 in "Too many open files") 
STATUS=0 
if [ $value -eq 11 ] || [ $value -eq 15 ] then 
if [ $CMD_OUTPUT -gt 500 ]
then
if [ $extension -lt 12 ]
then
STATUS=1 
fi
fi
fi

Can I use:

if (( ( $value -eq 11 || $value -eq 15 ) && ( $CMD_OUTPUT -gt 500 ) && ( $extension -lt 12 ) )) then;
STATUS=1

Is this a good way of doing it, will it optimize my requirement.

If in doubt correct all syntax errors even if the shell didn't spot them. You can get really strange effects.

On the performance front it would help to see how you are calling the script and in particular whether $3 contains wildcards. The only line which could be appreciably slow is the line which counts the records which match $4.
How big are your input file(s) - size and number of records ?
How many input files are there?

Do you call the script 9 times (once for every case statement) with different parameters? This could be the performance problem if every input file is searched 9 times for every run set of the script. If the input files are large it may be better to select all the records which interest you in one pass and then work on that subset.

How often do you run the script? Is it run from the command line, a control script, or from cron?