Whats wrong in the Function ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Whats wrong in the Function ?
# 1  
Old 10-04-2010
Whats wrong in the Function ?

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

Function usage erroring out even after passing parameters.

Code:
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.

Code:
$ ./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

# 2  
Old 10-04-2010
This:
Code:
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:
Code:
./xmlSearch.sh 0 0

would be accepted as valid input
# 3  
Old 10-04-2010
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.
# 4  
Old 10-04-2010
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:
Code:
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.
# 5  
Old 10-04-2010
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.
# 6  
Old 10-04-2010
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.

Code:
 
$ ./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.

# 7  
Old 10-04-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python - Function print vs return - whats wrong

All, I have a basic buzz program written in python with return function. If i change return with print,it works fine but i want to know whats wrong with return statement.Can anyone help me whats wrong with this #!/usr/bin/python def div4and6(s,e): for i in range(s,e+1): if... (5 Replies)
Discussion started by: oky
5 Replies

2. UNIX for Dummies Questions & Answers

Whats wrong with this if-else

hi whats wrong in below?? CHECK=M10; if ; then echo "hello hi"; else echo "how are u hello hi"; fi I am getting error as ./test.sh: line 2: ' ./test.sh: line 2: M10: command not found ./test.sh: line 2: M10: command not found ./test.sh: line 2: M10: command not found (8 Replies)
Discussion started by: skyineyes
8 Replies

3. Programming

Whats wrong with my my Makefile

its right only missed \ (0 Replies)
Discussion started by: dragonpoint
0 Replies

4. Homework & Coursework Questions

Whats wrong with the following

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: ls -ld htdocs drwxr-x--- 3 root root 8192 2006-11-19 10:41 htdocs How would a host administrator... (1 Reply)
Discussion started by: Larry_1
1 Replies

5. UNIX for Dummies Questions & Answers

whats wrong with this?

can anyone tell me why this code doesn't work how its supposed to, its the hangman game but it doesn't play how its supposed to #!/bin/bash NoAttempts="0" livesgiven="5" LivesRemain=$livesgiven LettersAttempted="" wordfile=words numwords=0 function menu() { clear cat << menu... (1 Reply)
Discussion started by: ferrycorsten73
1 Replies

6. UNIX for Dummies Questions & Answers

Whats wrong in the script?

if then if then echo "fst argument is $1 " else if then "fst argument is $1" fi fi fi Can anyone tell me. My requirement is tht pass a string .. Check whether it contains "-". If yes then check if it... (1 Reply)
Discussion started by: nehagupta2008
1 Replies

7. Shell Programming and Scripting

tell me whats wrong with this

#! /bin/bash USAGE=" | ] if then echo "$USAGE" exit 1 fi while getopts lb: OPTION do case $(OPTION)in a) echo Hi there! exit 2;; b) echo hello o) OARG=$OPTARG;; \?)echo "$USAGE" ;; exit 2;; esac done shift `expr... (1 Reply)
Discussion started by: nadman123
1 Replies

8. Shell Programming and Scripting

tell me whats wrong in this?

#! /bin/bash head -5 $1 echo "remove $1 ?" read answer if then echo invalid answer elif rm $1 echo "$1 is deleted" elif then echo file is not deleted else echo "invalid answer" fi What i really want this to do is to ask to delete the file or not..it says something wrong... (1 Reply)
Discussion started by: nadman123
1 Replies

9. UNIX for Advanced & Expert Users

Whats wrong in this Script ???

PATH="/clocal/mqbrkrs/user/mqsiadm/sanjay" MAIL_RECIPIENTS="xyz@abc.com" Subject="File accessed in last minutes:" find $PATH -type f -amin -1 > temp.txt.$$ cat temp.txt.$$ | \ while read line do fuser -uV $line >> tempmail.txt done cat "$tempmail.txt" | mailx -s "$Subject"... (4 Replies)
Discussion started by: varungupta
4 Replies

10. Shell Programming and Scripting

Whats wrong with my function?? <newbie>

First of all im using Bash, on a Debian-based machine. I tried to write a function that if the ls program found listed more than 25 lines I would automaticly use "ls | less". Its on another computer but if I recall it looked something like this... Note: some code may look strange because im on... (4 Replies)
Discussion started by: riwa
4 Replies
Login or Register to Ask a Question