Problem with CASE statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with CASE statement
# 1  
Old 02-15-2010
Tools Problem with CASE statement

Guys,

Here is the script syntax which is not accepting the parameters & not performing the said activity.

Code:
$ ./routing.sh xyz123-ra str enable

**********************************************************************
 Preparing to service the request for  Device xyz123-ra in Question
**********************************************************************

 Check USAGE for correct Parameters

USAGE
   routing.sh <device> <stp|str|disable|enable>

Here is the code for which the command variable is assigned for.

Code:
# Script name without path

BASENAME=`basename $0`

# Script Location

LOCATION=/home/myHome

#Default Firewall Values


# Host File Location

HOSTFILE=${LOCATION}/config/hosts

#The INFILE is going to use during the SSH

INFILE=${LOCATION}/config/inFile.txt

#The changes will write to OUTFILE

OUTFILE=${LOCATION}/out/outFile

# Functional Usage

usage() {
        echo
        echo "USAGE"
        echo "   "`basename $0`" <device> <stp|str|disable|enable> "
        echo
        exit 1
        }
        echo
        echo "**********************************************************************"
        echo " Preparing to service the request for  Device ${1} in Question "
        echo "**********************************************************************"
        echo
        if [ "$2" != 0 ]|| [ "$1" != 0 ]; then
        echo " Check USAGE for correct Parameters "
        else
        echo " Script aheading to perform the activity "
        fi

Firewall() {

       #Local Variable
       #
        local STATE="$1"
       #

cat << EOF > $INFILE
admin
passwd
clock
switch Services
config
firewall Firewall
admin  ${STATE}
show
exit
write memory
y
exit
exit
EOF
}

Traffic_Router() {

      #Local Variables
      #
       local STATE="$1"
      #

cat << EOF > $INFILE
admin
passwd
clock
switch Services
config
router Traffic_Router
admin ${STATE}
show
exit
write memory
y
exit
exit
EOF
}

#
#Validate the device name
#
echo $1 |fgrep "$1" $HOSTFILE > /dev/null || {
echo " Invalid Server Request "
exit 1
}

DPDEVICE="$1"

# Check the  parameters
#
[ $# -ne 2 ] && usage


# Check the input parameters
#

case $2 in

         stp)
            Firewall disabled
            echo " Changing the http Proxy configuration "
        ;;
         str)
            Firewall enabled
            echo " Setting right http Proxy configuration "
        ;;
         disable)
                Traffic_Router disabled
                echo " Disabling the Device in few seconds "
        ;;
         enable)
                Traffic_Router enabled
                echo " Enabling the Device in few seconds "
        ;;
        *)
                usage
                echo " Please check for correct Parameter from the Usage "
        ;;
esac

From the above code the command variable {stp & str } is not being considered as one of the input parameter & not performing the activity for which it is assigned for. Only the {disable & enable } is performing.

Can someone find me the bug in this code.
# 2  
Old 02-15-2010
Quote:
Originally Posted by raghunsi
Here is the script syntax which is not accepting the parameters & not performing the said activity.
Code:
$ ./routing.sh xyz123-ra str enable

**********************************************************************
 Preparing to service the request for  Device xyz123-ra in Question
**********************************************************************

 Check USAGE for correct Parameters

USAGE
   routing.sh <device> <stp|str|disable|enable>


You called the script with three parameters, but it only accepts two.
Quote:

Here is the code for which the command variable is assigned for.

Code:
# Script name without path

BASENAME=`basename $0`


There's no need for the basename command in a POSIX shell; use shell parameter expansion:
Code:
BASENAME=${0##*/}

And there is certainly no need to use basename twice.
Quote:
Code:
# Script Location

LOCATION=/home/myHome

#Default Firewall Values


# Host File Location

HOSTFILE=${LOCATION}/config/hosts

#The INFILE is going to use during the SSH

INFILE=${LOCATION}/config/inFile.txt

#The changes will write to OUTFILE

OUTFILE=${LOCATION}/out/outFile

# Functional Usage

usage() {
        echo
        echo "USAGE"
        echo "   "`basename $0`" <device> <stp|str|disable|enable> "
        echo
        exit 1
        }
        echo
        echo "**********************************************************************"
        echo " Preparing to service the request for  Device ${1} in Question "
        echo "**********************************************************************"
        echo
        if [ "$2" != 0 ]|| [ "$1" != 0 ]; then
        echo " Check USAGE for correct Parameters "
        else
        echo " Script aheading to perform the activity "
        fi

Firewall() {

       #Local Variable
       #
        local STATE="$1"
       #

cat << EOF > $INFILE


Why are you using cat? Why not print the lines directly?
Code:
printf "%s\n" "admin
passwd
clock
switch Services
config
firewall Firewall
admin  $STATE
show
exit
write memory
y
exit
exit" > "$INFILE"

Quote:
Code:
admin
passwd
clock
switch Services
config
firewall Firewall
admin  ${STATE}
show
exit
write memory
y
exit
exit
EOF
}

Traffic_Router() {

      #Local Variables
      #
       local STATE="$1"
      #

cat << EOF > $INFILE


See above.
Quote:
Code:
admin
passwd
clock
switch Services
config
router Traffic_Router
admin ${STATE}
show
exit
write memory
y
exit
exit
EOF
}

#
#Validate the device name
#
echo $1 |fgrep "$1" $HOSTFILE > /dev/null || {


Why are you piping something to fgrep and giving a filename? You can't do both.
Code:
fgrep "$1" $HOSTFILE > /dev/null || {

Quote:
Code:
echo " Invalid Server Request "
exit 1
}

DPDEVICE="$1"


The quotes are not necessary:
Code:
DPDEVICE=$1

Quote:
Code:
# Check the  parameters
#
[ $# -ne 2 ] && usage

# Check the input parameters
#

case $2 in

         stp)
            Firewall disabled
            echo " Changing the http Proxy configuration "
        ;;
         str)
            Firewall enabled
            echo " Setting right http Proxy configuration "
        ;;
         disable)
                Traffic_Router disabled
                echo " Disabling the Device in few seconds "
        ;;
         enable)
                Traffic_Router enabled
                echo " Enabling the Device in few seconds "
        ;;
        *)
                usage
                echo " Please check for correct Parameter from the Usage "
        ;;
esac

From the above code the command variable {stp & str } is not being considered as one of the input parameter & not performing the activity for which it is assigned for. Only the {disable & enable } is performing.

Can someone find me the bug in this code.
# 3  
Old 02-16-2010
Changed the code as per the instruction but still the script erroring out.

Code:
$ ./DPoutrotate.sh tdad6324-ra str enable
./DPoutrotate.sh: line 40: syntax error near unexpected token `<'
./DPoutrotate.sh: line 40: `        echo "   `${0##*/}`" <device> <stp|str|disable|enable> "'

The changed code is here :

Code:
# Script name without path

BASENAME= ${0##*/}

#Default Firewall Values


# Host File Location

HOSTFILE=${LOCATION}/config/hosts

#The INFILE is going to use during the SSH

INFILE=${LOCATION}/config/inFile.txt

#The changes will write to OUTFILE

OUTFILE=${LOCATION}/out/outFile

# Functional Usage

usage() {
        echo
        echo "USAGE"
        echo "   "`${0##*/}`" <device> <stp|str|disable|enable> "
        echo
        exit 1
        }
        echo
        echo "**********************************************************************"
        echo " Preparing to service the request for  Device ${1} in Question "
        echo "**********************************************************************"
        echo
        if [ "$2" != 0 ]|| [ "$1" != 0 ]; then
        echo " Check USAGE for correct Parameters "
        else
        echo " Script aheading to perform the activity "
        fi

Firewall() {

       #Local Variable
       #
        local STATE="$1"
       #

printf "%s\n" "admin
passwd
clock
switch Services
config
firewall Firewall
admin  ${STATE}
show
exit
write memory
y
exit
exit" > "$INFILE"
}

Traffic_Router() {

      #Local Variables
      #
       local STATE="$1"
      #

printf "%s\n" "admin
admin
passwd
clock
switch Services
config
router Traffic_Router
admin ${STATE}
show
exit
write memory
y
exit
exit" > "$INFILE"
}

#
#Validate the device name
#
fgrep "$1" $HOSTFILE > /dev/null || {
echo  Invalid Server Request 
exit 1
}

DPDEVICE=$1

# Check the  parameters
#
[ $# -ne 2 ] && usage


# Check the input parameters
#

case $2 in

         stp)
            Firewall disabled
            echo " Changing the http Proxy configuration "
        ;;
         str)
            Firewall enabled
            echo " Setting right http Proxy configuration "
        ;;
         disable)
                Traffic_Router disabled
                echo " Disabling the Device in few seconds "
        ;;
         enable)
                Traffic_Router enabled
                echo " Enabling the Device in few seconds "
        ;;
        *)
                usage
                echo " Please check for correct Parameter from the Usage "
        ;;
esac

# 4  
Old 02-16-2010
Quote:
Originally Posted by raghunsi
Changed the code as per the instruction but still the script erroring out.

Code:
$ ./DPoutrotate.sh tdad6324-ra str enable
./DPoutrotate.sh: line 40: syntax error near unexpected token `<'
./DPoutrotate.sh: line 40: `        echo "   `${0##*/}`" <device> <stp|str|disable|enable> "'

The changed code is here :

Code:
# Script name without path

BASENAME= ${0##*/}


Error. There should be no space after '='.
Quote:
Code:
#Default Firewall Values


# Host File Location

HOSTFILE=${LOCATION}/config/hosts


Where have you defined LOCATION?

(The braces are not needed.)
Quote:
Code:
#The INFILE is going to use during the SSH

INFILE=${LOCATION}/config/inFile.txt

#The changes will write to OUTFILE

OUTFILE=${LOCATION}/out/outFile

# Functional Usage

usage() {
        echo
        echo "USAGE"
        echo "   "`${0##*/}`" <device> <stp|str|disable|enable> "


Do you really want the output from rerunning the command? You'll end up calling it recursively and the script will never end.

And why are you extracting the name of the command again? You already have it in a variable.
Code:
echo "   $BASENAME <device> <stp|str|disable|enable> "

Quote:
Code:
        echo
        exit 1
        }


The following indentation is confusing; it should start in the first column, not lined up with the function's closing brace.
Quote:
Code:
        echo
        echo "**********************************************************************"
        echo " Preparing to service the request for  Device ${1} in Question "
        echo "**********************************************************************"
        echo
        if [ "$2" != 0 ]|| [ "$1" != 0 ]; then
        echo " Check USAGE for correct Parameters "
        else
        echo " Script aheading to perform the activity "
        fi

Firewall() {

       #Local Variable
       #
        local STATE="$1"
       #

printf "%s\n" "admin
passwd
clock
switch Services
config
firewall Firewall
admin  ${STATE}
show
exit
write memory
y
exit
exit" > "$INFILE"
}

Traffic_Router() {

      #Local Variables
      #
       local STATE="$1"
      #

printf "%s\n" "admin
admin
passwd
clock
switch Services
config
router Traffic_Router
admin ${STATE}
show
exit
write memory
y
exit
exit" > "$INFILE"
}

#
#Validate the device name
#
fgrep "$1" $HOSTFILE > /dev/null || {
echo  Invalid Server Request 
exit 1
}

DPDEVICE=$1

# Check the  parameters
#
[ $# -ne 2 ] && usage


# Check the input parameters
#

case $2 in

         stp)
            Firewall disabled
            echo " Changing the http Proxy configuration "
        ;;
         str)
            Firewall enabled
            echo " Setting right http Proxy configuration "
        ;;
         disable)
                Traffic_Router disabled
                echo " Disabling the Device in few seconds "
        ;;
         enable)
                Traffic_Router enabled
                echo " Enabling the Device in few seconds "
        ;;
        *)
                usage
                echo " Please check for correct Parameter from the Usage "
        ;;
esac

# 5  
Old 02-16-2010
Johnson,

Again the same issue, the script is not considering the {str or stp} & {disable or enable} variable command simultaneously. It consider any one command at a time .. for Example just given:

Code:
This syntax is using only one Variable command {str or stp} 

$ ./routing.sh xyz123-ra  str
 Setting right http Proxy configuration
 Report Logging . .

 Tue Feb 16 01:20:20 CST 2010

 FILE CREATED:  /home/myHome/out/outFile100216-012020.xyz123-ra.

 Global configuration mode
 Modify Firewall Service configuration
  admin-state enabled
 Configuration saved successfully.
 Goodbye

*********************************************************************

Code:
This syntax is using both the variable Command {str & enable}. 

$ ./routing.sh tdad6324-ra  str enable

USAGE
  routing.sh <device> <stp|str|disable|enable>

Code:
This Syntax is using only one Variable command {enable or disable}

$ ./routing.sh xyz123-ra  enable
 Enabling the Device in few seconds
 Report Logging . .

 Tue Feb 16 01:13:30 CST 2010

 FILE CREATED:  /home/myHome/out/outFile100216-011330.xyz123-ra.

 Global configuration mode
 Modify Traffic_Router configuration
  admin-state enabled
 Configuration saved successfully.
 Goodbye

*********************************************************************

Where the script is erroring out ?? Why its not accepting two varaible commands simultaenously ??

I want script should accept str & enable or stp & disable or criss cross.
# 6  
Old 02-16-2010
Quote:
Originally Posted by raghunsi
Where the script is erroring out ?? Why its not accepting two varaible commands simultaenously ??

Because you are coding it only to accept 2 arguments:
Code:
[ $# -ne 2 ] && usage

You are calling the script with 3 arguments:
Code:
$ ./routing.sh tdad6324-ra str enable

The 3 arguments are 'tdad6324-ra', 'str', and 'enable'.
# 7  
Old 02-16-2010
Johnson,

But still script's third argument {enable or disable} is not working !! Im feeling i need to insert OPTARGS for this purpose.

Please check this output of the script:

Code:
$ ./routing.sh xyz123-ra  str enable or disable

**********************************************************
 Preparing to service the request for  Device xyz123-ra in Question
**********************************************************

 Setting right http Proxy configuration
 Report Logging . .

 Tue Feb 16 07:47:24 CST 2010

 FILE CREATED:  /home/jg355187/out/outFile100216-074724.xyz123-ra.enable

 Global configuration mode
 Modify Firewall Service configuration
 admin-state enabled
 Configuration saved successfully.
 Goodbye

**********************************************************

This part of the code is not functioning Properly

Code:
##############################################

printf "%s\n" "admin
passwd
clock
switch Services
config
xmlfirewall Firewall
admin-state ${STATE}
show
exit
write memory
y
exit
exit" > "$INFILE"
}

##################################################

Check the input parameters
#
   case $2 in

         stp)
            Firewall disabled
            echo " Changing the http Proxy configuration "
        ;;
         str)
            Firewall enabled
            echo " Setting right http Proxy configuration "
        ;;
         disable)
                Traffic_Router disabled
                echo " Disabling the Device in few seconds "
        ;;
          enable)
                Traffic_Router enabled
                echo " Enabling the Device in few seconds "
        ;;

###################################################

The highlighted part of the CASE statement is very much needed here in this script, the purpose of this building the script is based on Traffic Routing which is not functioning properly. Do let me know whether my idea of inserting OPTRAGS is legible or not ??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Strange Problem in case statement while shift

Hi, Here is my code as below: test.ksh: ======= #!/bin/ksh option="${1}" while do case $1 in -f) FILE="${2}" echo "File name is $FILE" ;; -d) DIR="${2}" echo "Dir name is $DIR" ;; -*) echo "`basename ${0}`:usage: | " (5 Replies)
Discussion started by: zaq1xsw2
5 Replies

2. Homework & Coursework Questions

Case Statement

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: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

3. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Homework & Coursework Questions

Problem with executing a possible if or case statement script

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: Create a phonebook program. It should use functions to perform the required tasks. It should be menu-based,... (1 Reply)
Discussion started by: Rgasin02
1 Replies

5. Shell Programming and Scripting

Problem using 'Case' statement

Hi, When I execute the below script, I am getting the error as ' is not expected.ror at line 3 : `in #!/bin/sh case $1 in -r) echo Force deletion without confirmation ;; -i) echo Confirm before deleting ;; *) echo Unknown argument ;; esac I could not see any problem with... (1 Reply)
Discussion started by: Sathish_Obla
1 Replies

6. Shell Programming and Scripting

Problem with case statement

Hi, I need modify the all lines in a file into one format. cat file htec.twe34c.ATI .hesh.twdghu..ATI ..hesh.twdghu..ATI htec.twe3 hjsct14567ati Output should have 16 characters htectwe34c ATI heshtwdghu ATI heshtwdghu ATI htectwe3 ATI hjsct14567 ATI (4 Replies)
Discussion started by: kartheek
4 Replies

7. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

8. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

9. UNIX for Advanced & Expert Users

Case statement problem

I need to display this menu and accept variables. Can someone tell me why i am having a problem with this case statement, please # TAPE MANAGER MAIN MENU tapemgr_Main_Menu() { echo "Legato Tape Management System Menu" echo " This system is used to report Legato ERV Offsite and Tapes... (6 Replies)
Discussion started by: gzs553
6 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question