Whats wrong in the Function ?

Need your assistance, to find the bug in the function.

Function usage erroring out even after passing parameters.

usage() {

        if [ "$1" != 0 ] || [ "$2" != 0 ]; then
        echo "************************************************************"
        echo "          CHECK USAGE FOR CORRECT PARAMETERS                "
        echo "************************************************************"
        echo
        echo "  USAGE:   "
        echo "  $BASENAME  <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME> = Enter the vaild SERVICE_NAME that exists in 76S DIR.
  <-L | -l>      = Searching from the LIVE LOGS.
  <-A | -a>      = Searching from the ARCHIVE LOGS."
        else
        echo
        echo "***********************************************************************************************"
        echo "          Searching for the "$2" XML in Request                                                  "
        echo "***********************************************************************************************"
        echo
        fi
}
usage
exit


#Validate the Service Name from
#
function serachFilePattern() {
#check for service name in Filepattern depending on the option(Live/Archive)
#

        case $OPTRG in
                -L | -l)
                        ServiceName=`egrep -i $SERVICE_NAME ${liveFilePattern}`
                        status=$?
                        if [ $status -ne 1 ]
                        then
                        echo "              "
                        echo "***Found the service logging in APP***"
                        cd ${LOG_DIR}
                        stat=$?
                        if [ $stat -ne 1 ]
                        then
                        echo "              "
                        echo "Change directory to path : `pwd` is SUCCESSFULL"
                        echo "              "
                        fi
                        while read mLine
                        do
                        n="1 2 3 4 5"
                        logFile="${SERVICE_NAME}_LoggerGroup"
                        for i in $n
                        do
                        echo "**********************************************************"
                        echo "My line: $mLine"
                        searchString=`egrep -i "$mLine" "${logFile}$i"`
                        status=$?
                        echo "**********************************************************"
                        if [ $status -ne 1 ]
                        then
                        echo "Search string Found $i: ${searchString}"
                        else
                        echo "Search String not found in $i"
                        fi
                        done
                        done < "${myString}"
                        else
                        echo "String does not exist in INPUT FILE or FILE IS EMPTY "
                        fi
                ;;
                -A | -a)
                       ServiceName=`egrep -i $SERVICE_NAME ${archFilePattern}`
                        status=$?
                        if [ $status -ne 1 ]
                        then
                        echo "              "
                        echo "***Found the service logging in APP ***"
                        cd ${LOG_DIR}
                        stat=$?
                        if [ $stat -ne 1 ]
                        then
                        echo "              "
                        echo "Change directory to path : `pwd` is SUCCESSFULL"
                        echo "              "
                        fi
                        while read mLine
                        do
                        n="1 2 3 4 5"
                        logFile="${SERVICE_NAME}_LoggerGroup"
                        for i in $n
                        do
                        echo "**********************************************************"
                        echo "My line: $mLine"
                        searchString=`gzegrep -i "$mLine" ${logFile}$i*.log.gz`
                        status=$?
                        echo "**********************************************************"
                        if [ $status -ne 1 ]
                        then
                        echo "Search string Found $i: ${searchString}"
                        else
                        echo "Search String not found in $i"
                        fi
                        done
                        done < "${myString}"
                        else
                        echo "String does not exist in INPUT FILE or FILE IS EMPTY"
                        fi
              ;;
              *)
                       usage
                       echo " Please check for correct Parameter from the Usage "
              ;;
            esac
   }
serachFilePattern
 exit

Output of the Script when using all the parameters.

$ ./xmlSearch.sh -l SERVICE_NAME
************************************************************
          CHECK USAGE FOR CORRECT PARAMETERS
************************************************************

  USAGE:
  xmlSearch.sh  <OPTION> <SERVICE_NAME>

  <SERVICE_NAME> = Enter the vaild SERVICE_NAME that exists in APP DIR.
  <-L | -l>      = Searching from the LIVE LOGS.
  <-A | -a>      = Searching from the ARCHIVE LOGS.

$ echo $?
0

This:

if [ "$1" != 0 ] || [ "$2" != 0 ]; then

means if either $1 or $2 is unequal to the string (not the value) "0". So only this input:

./xmlSearch.sh 0 0

would be accepted as valid input

My concern is, the script should be pased with 2 parameters, if any of the Parameter is missing or misinterpreted, it should show an USAGE. But from the above example, it has all 2 mandatory parameters passed, but its still showing the USAGE, instead executing the search.

Yes but your script checks whether the parameters are equal to the string that contains the number zero. I would remove the if statement out of your usage function and do something like this:

if [ $# != 2 ]; then
  usage
fi

at the start of your script. $# means the number of parameters. Also, you might want to add an exit statement inside the function.

Looks like a fundamental understanding problem.

$1 and $2 within a function refer to the parameters supplied to that function. They are not the $1 and $2 parameters provided to the main script.

The convention for a function called "usage" is to just output a message telling the user how to correctly use the script. It is also conventional to "exit" the script if there is something wrong.

You would be best testing the number of parameters $# in the main flow of the script and calling a "usage" function if the number of parameters is incorrect. If you then do more validatation you then have available a general function to report the error.

Methyl, I stand by with you. This USAGE is something providing the user about the Function and aprameter of the script built for.

But my script is looping with the USAGE still now. I have got no hints where needs it needs to be changed.

Whenever I run with all the Input Parameters, the script outputs the same result as what without the Input Parameters provides. Even when i check its exit status, it showing sucess. But no idea where is the real bug exists in that.

Here is an output after making changes as what "Scrutinizer' has said previuosly.

 
$ ./xmlSearch.sh -l SERVICE_NAME
************************************************************
          CHECK USAGE FOR CORRECT PARAMETERS
************************************************************

  USAGE:
  xmlSearch.sh  <OPTION> <SERVICE_NAME>

  <SERVICE_NAME> = Enter the vaild SERVICE_NAME that exists in APP DIR.
  <-L | -l>      = Searching from the LIVE LOGS.
  <-A | -a>      = Searching from the ARCHIVE LOGS.

I seem to recall a thread about this before. Are you two posters?

I said it then, and say it now: That code shouldn't be in a function. $1, $#, etc. don't do what you expect them to to because you're in a function. Inside a function they refer to function arguments, not script ones.

can you please provide me the link of previous thread, so that i could able to correct the script. But one thing is mesmerising me that, previuosly i had the same structured script, which works well, but this has become ache . :frowning:

I did, in my previous post.

Don't check for arguments in the function, just print the usage in the function. Check the arguments outside the function where $1 and $# do what you think they do.

The development of this script by this board has gone on for some time without clear specification of the requirement.

If the current poster cannot remember previous handles, then this thread is dead.

Yes Corona, Now the function has only echoing USAGE statement and checking for argument is outside the Function Call. But still the result is the same NO change in it.

Here it is:

function usage (){
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo    "USAGE:"
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in 76S DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
        echo "  Please check for correct Parameter from the Usage "
        echo
}
usage
exit


#Check Input parameters are valid

        if [ $1 != 0 ] || [ $2 != 0 ]; then
        usage
        fi

I tried even $# but its the same result.

Result is here.

$ ./testxmlsearch.sh -a dp_3yl_to_choicepoint

*******************************************
       !!! USAGE PARAMETERS !!!
*******************************************


USAGE:
   testxmlsearch.sh <OPTION> <SERVICE_NAME>

  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in 76S DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS.

  Please check for correct Parameter from the Usage

your calling usage before doing the test here is what you need:

function usage (){
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo    "USAGE:"
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in 76S DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
        echo "  Please check for correct Parameter from the Usage "
        echo
        exit
}


#Check Input parameters are valid
if [ $# != 2 ]; then
        usage
fi

In my previous trial, i tested the usage there after. But even that time i had same issue.
Can you rewrite the the same ??

Yes I already have look at the code in post #12

Look raghunsi,

In your latest trial you used this:

if [ $1 != 0 ] || [ $2 != 0 ]; then
  usage
fi

while already in post #2 it was pointed out to you (and in various other posts in this thread) that this is not correct and suggested you use this instead:

if [ $# != 2 ]; then
        usage
fi

Please have a good look at the suggestions and take your time experimenting. And then come back here once you tried all the suggestions...

I have tried using the above method as it was suggested before. There is no change in the result.. So here it is

function usage() {

        if [ $# != 2 ]; then
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo    "USAGE:"
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in APPS DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
        fi
}
usage
exit

Result ,

$ ./testxmlsearch.sh -a SERVICE_NAME

*******************************************
       !!! USAGE PARAMETERS !!!
*******************************************


USAGE:
   testxmlsearch.sh <OPTION> <SERVICE_NAME>

  <SERVICE_NAME>   = Enter the valid SERVICE_NAME that exists in APPS DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS.

The above result is only valid when any of the input arguments are missing, but not in this scenario when the script has passed with valid arguments to search.

Thanks in advance, if can get a break through.

---------- Post updated at 07:48 AM ---------- Previous update was at 07:23 AM ----------

SCENARIO 1: Here is the Result when I changed the USAGE function as below

function usage() {

        if [ $# != 2 ]; then
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo    "USAGE:"
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in APPS DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
        fi
        exit
}

This is a VALID OUTPUT when passing both (OPTION & SERVICE_NAME) the argument ---> 2 Arguments Passed for the script

$ ./testxmlsearch.sh -a SERVICE_NAME

***Found the service logging in APPS***

Change directory to path : /logs/production/Apps/logs is SUCCESSFULL

**********************************************************
My line: MsgID=414d51204450303141504420202020204c7e897a20374b16
**********************************************************
Search String not found in 1
**********************************************************
My line: MsgID=414d51204450303141504420202020204c7e897a20374b16
**********************************************************
Search String not found in 2
**********************************************************
My line: MsgID=414d51204450303141504420202020204c7e897a20374b16
**********************************************************
Search String not found in 3
**********************************************************
My line: MsgID=414d51204450303141504420202020204c7e897a20374b16
**********************************************************
Search String not found in 4
**********************************************************
My line: MsgID=414d51204450303141504420202020204c7e897a20374b16
**********************************************************
Search String not found in 5

BUT the result for any one argument is INVALID and still looping, giving NO PROMT ----> 1 Argument passed for the script
Instead it should throw the USAGE Parameters

$ ./testxmlsearch.sh -a
^C
$

SCENARIO 2: Here is the Result when I changed the USAGE function as below

function usage() {

        if [ $# != 2 ]; then
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo    "USAGE:"
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in APPS DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
        fi
}
usage
exit

This is a INVALID OUTPUT when passing both (OPTION & SERVICE_NAME) the argument ---> 2 Arguments are passed to script

$ ./testxmlsearch.sh -a SERVICE_NAME 

*******************************************
       !!! USAGE PARAMETERS !!!
*******************************************


USAGE:
   testxmlsearch.sh <OPTION> <SERVICE_NAME>

  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in APPS DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS.

As stated by various users in this thread if [ $# != 2 ]; then should not be used inside the usage function.

1 Like
1 Like

Change the code.

if [ $# != 2 ]; then
function usage() {
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in 76S DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
        fi
}
usage
exit

Result after change even after closing all the functions

$ ./testxmlsearch.sh -a SERVICE_NAME
./testxmlsearch.sh: line 54: syntax error near unexpected token `fi'
./testxmlsearch.sh: line 54: `        fi'
$./testxmlsearch.sh -a 
./testxmlsearch.sh: line 54: syntax error near unexpected token `fi'
./testxmlsearch.sh: line 54: `        fi'

---------- Post updated at 08:27 AM ---------- Previous update was at 08:20 AM ----------

Thanks for all . The script is Working really good now.

THE FINAL CHANGE IS HERE

if [ $# != 2 ]; then
function usage() {
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in 76S DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
}
usage
exit
fi

Better form is like this:

########### Subroutines

function usage() {
        echo
        echo "*******************************************"
        echo "       !!! USAGE PARAMETERS !!!            "
        echo "*******************************************"
        echo
        echo
        echo "  " `basename $0`" <OPTION> <SERVICE_NAME>  "
        echo
        echo "  <SERVICE_NAME>   = Enter the vaild SERVICE_NAME that exists in 76S DIR.
    <-L | -l>      = Searching from the LIVE LOGS.
    <-A | -a>      = Searching from the ARCHIVE LOGS."
        echo
}

########## MAIN

if [ $# != 2 ]; then
usage
exit
fi