The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed error : Syntax error: redirection unexpected phpfreak Shell Programming and Scripting 3 12-04-2008 05:19 AM
nim mksysb error :/usr/bin/savevg[33]: 1016,07: syntax error astjen AIX 9 10-03-2008 12:44 PM
awk Shell Script error : "Syntax Error : `Split' unexpected Herry UNIX for Dummies Questions & Answers 2 03-17-2008 11:16 AM
syntax issue in ksh file manav666 Shell Programming and Scripting 2 10-30-2007 07:52 AM
I got error like...syntax error on line 1, teletype koti_rama UNIX for Advanced & Expert Users 2 07-07-2007 08:35 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-26-2009
warlock129 warlock129 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 38
Help understanding syntax error Issue

Hi i as you may already know i am creating a menu driven program. I have chosen to take the approach of implementing each interface individually, after adding another interface and attempting to run the program i am faced with the following error:

./Assigntest: line 32: syntax error near unexpected token `)'
./Assigntest: line 32: `2)clear'

The code i am working is included below and i have highlighted in red the newly added section where i think the error is being generated from.


Code:
#!/bin/bash
#Filename: Assigntest Author: Luke Francis
quit=n
while [ "$quit" = "n" ]
do
clear
echo "OPERATOR ADMINISTRATIVE TOOL"
echo "1. User Information"
echo "2. Network Connectivity"
echo "3. Processes"
echo "4. System Information"
echo "5. Hardware Utilization"
echo "Q. Quit"
echo
echo "Which option do you require?"

read menunumber
case $menunumber in
1)clear
echo "USER INFORMATION"
 echo "1. Registered Users"
 echo "2. Disk Usage"
 echo "3. Last Logins"
 echo "4. Users Currently Logged In"
 echo "5. Total number of users"
 echo "Q. Quit"
echo "Which option do you require?"

2)clear
echo "NETWORK CONNECTIVITY"
echo "1. NIC Status"
echo "2. Machine Availability"
echo "Which option do you require?"

read menunumber2
case $menunumber2 in

1)clear
echo "The users registered on the system are:"
echo
awk -F: '{print $1}' /etc/passwd
echo
echo "Hit the Enter Key to continue"
read junk;;

2)clear
echo "Disk Usage is as follows:"
echo
du
echo
echo "Hit Enter Key to continue"
read junk;;

3)clear
echo "Information on last noted login can be found next to each username."
echo
last
echo
echo "Hit Enter Key to continue"
read junk;;

4)clear
echo "Users currently logged in are:"
echo
 w
echo
echo "Hit Enter Key to continue"
read junk;;

5)clear
echo "The total number of users are:"
echo
 who -q
echo
echo "Hit Enter Key to continue"
read junk;;

Q|q)clear
echo "Are you sure you want to quit? Y/N"
read choice1
case $choice1 in
N|n)clear
echo "Hit Enter Key to continue"
read junk;;

Y|y)quit=y;;
*)clear
sleep 1;;
esac
;;
esac
;;
Q|q)clear
echo "Are you sure you want to quit? Y/N"
read choice2
case $choice2 in
N|n)clear
echo "Hit Enter key to continue"
read junk;;

Y|y)quit=y;;
*)clear
sleep 2;;
esac
esac
done
clear
echo "Thank you for using the Operator Administrative Tool"


If someone could please ammend this for me and explain where i am going wrong then i could apply this to further interfaces and would also decrease number of posts from me (not that i dont like this forum lol). Thanks
  #2 (permalink)  
Old 04-26-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Please make sure that the syntax for case is implemented correct.


Code:
case expression in
pattern1) commands1 ;;
pattern2) commands2 ;;
pattern3) commands3 ;;
...
esac


cheers,
Devaraj Takhellambam
  #3 (permalink)  
Old 04-26-2009
warlock129 warlock129 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 38
Sorry i do not quite understand your answer if possible could you please ammend it in the code i posted earlier so i can get a better idea for what you mean. I really appreciate this.
  #4 (permalink)  
Old 04-26-2009
giannicello giannicello is offline
Registered User
  
 

Join Date: Sep 2001
Location: Phoenix
Posts: 169
Once you align your code correctly so you will be able to see where each case begins and ends, you will then also see where to correctly terminate your options and case statements.
For example:


Code:
         Q|q)clear
           echo "Are you sure you want to quit? Y/N"
           read choice1
           case $choice1 in
              N|n)clear
                 echo "Hit Enter Key to continue"
                 read junk;;

              Y|y)quit=y;;
              *)clear
                sleep 1;;
           esac
         ;; #This one finishes off quit option.
         esac  # This one end case for registered user options
       ;; # This one end case for main menu #1
      2)clear
        echo "NETWORK CONNECTIVITY"
        echo "1. NIC Status"
        echo "2. Machine Availability
        echo "Which option do you require?"

        read menunumber2
        case $menunumber2 in

          1)clear
            echo "1. NIC Status"
            echo
            awk -F: '{print $1}' /etc/passwd
            echo
            echo "Hit the Enter Key to continue"
            read junk;;

          2)clear
            echo "2. Machine Availability"
            echo
            du
            echo
            echo "Hit Enter Key to continue"
            read junk;;

          Q|q)clear
            echo "Are you sure you want to quit? Y/N"
            read choice1
            case $choice1 in
                 N|n)clear
                     echo "Hit Enter Key to continue"
                     read junk;;

                 Y|y)quit=y;;
                 *)clear 
                   sleep 1;;
            esac
            ;; # This one finishes off quit option.
        esac # This one end case for network connectivity options
         ;; # This one end case for main menu #2

        3)echo Option 3 here...
          sleep 2
         ;;
        4)echo Option 4 here...
          sleep 2
         ;;
        5)echo Option 5 here...
          sleep 2
         ;;
        Q|q)echo "Quitting...finally"
          exit 0
          ;;
      esac
done
clear
echo "Thank you for using the Operator Administrative Tool"

  #5 (permalink)  
Old 04-26-2009
warlock129 warlock129 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 38
Ok i have taken your advised indented my code and i have managed to fix my problem but unfortuantely now another small one has arisen.

The problem is that executing my commands requires two presses of the ENTER key as opposed to the originally being pressed once as one would expect, for example when quitting the program you are asked "Are you sure you want to quit?" after pressing Y you then have to press enter twice before the program is closed whereas before i was once you would expect. My edited code has been included below:


Code:
#!/bin/bash
#Filename: Assigntest Author: Luke Francis
quit=n
while [ "$quit" = "n" ]
do
clear
  echo "OPERATOR ADMINISTRATIVE TOOL"
   echo "1. User Information"
    echo "2. Network Connectivity"
    echo "3. Processes"
     echo "4. System Information"
      echo "5. Hardware Utilization"
       echo "Q. Quit"
       echo
        echo "Which option do you require?"

read menunumber
case $menunumber in
1)clear
    echo "USER INFORMATION"
     echo "1. Registered Users"
      echo "2. Disk Usage"
       echo "3. Last Logins"
        echo "4. Users Currently Logged In"
         echo "5. Total number of users"
          echo "Q. Quit"
          echo "Which option do you require?"

read menunumber2
case $menunumber2 in

        1)clear
          echo "The users registered on the system are:"
          echo
          awk -F: '{print $1}' /etc/passwd
          echo
          echo "Hit the Enter Key to continue"
          read junk;;
2)clear
          echo "Disk Usage is as follows:"
          echo
          du
          echo
          echo "Hit Enter Key to continue"
          read junk;;

        3)clear
          echo "Information on last noted login can be found next to each username."
          echo
          last
          echo
          echo "Hit Enter Key to continue"
          read junk;;

        4)clear
          echo "Users currently logged in are:"
          echo
          w
          echo
          echo "Hit Enter Key to continue"
          read junk;;

        5)clear
          echo "The total number of users are:"
          echo
          who -q
          echo
          echo "Hit Enter Key to continue"
          read junk;;

        Q|q)clear
          echo "Are you sure you want to quit? Y/N"
          read choice1
          case $choice1 in
               N|n)clear
                   echo "Hit Enter Key to continue"
read junk;;

               Y|y)quit=y;;
               *)clear
                 sleep 1;;
          esac
          ;;
        esac
;;
        Q|q)clear
          echo "Are you sure you want to quit? Y/N"
read choice2
case $choice2 in
        N|n)clear
            echo "Hit Enter key to continue"
                read junk;;

        Y|y)quit=y;;
        *)clear
           sleep 2;;
                esac
             ;;
                esac

read menunumber3
case $menunumber3 in

        2)clear
          echo "NETWORK CONNECTIVITY"
          echo
          echo "1. NIC Status"
          echo "2. Machine Availability"
          echo
          echo "Which option do you require?"

read menunumber4
case $menunumber4 in
1)clear
          echo "Information reagrding NIC status can be found below:"
          echo
          /sbin/ifconfig
          echo
          echo "Hit the Enter Key to continue"
          read junk;;

        2) clear
           echo "Available hosts and addresses are shown below:"
           echo
           cat /etc/hosts
           echo
           echo "Hit Enter Key to continue"
           read junk;;
        esac
       esac
done
clear
echo "Thank you for using the Operator Administrative Tool"

I hope you can understand what i am trying to get across, if not just say so i can explain. Also if possible could you please add any ammendmants recommended into the code i have supplied so i know exactly where to place them. Thanks alot for your help.
  #6 (permalink)  
Old 04-26-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 548
Quote:
Originally Posted by warlock129 View Post
...
The problem is that executing my commands requires two presses of the ENTER key as opposed to the originally being pressed once as one would expect, for example when quitting the program you are asked "Are you sure you want to quit?" after pressing Y you then have to press enter twice before the program is closed whereas before i was once you would expect.
...
That's because the script is logically flawed. After displaying the initial menu with the header "OPERATOR ADMINISTRATIVE TOOL", you are capturing the user's choice twice - once in "menunumber" and the second time in "menunumber3". Ideally, you should capture the choice in one variable only and then compare this value using the case construct.

Here's the modified script with proper indentation. Note how the user's input is captured *only* in one variable - "menunumber", which is then compared to 1, 2, and Q|q.

Of course, you will have add the appropriate menus and sub-hierarchies for 3, 4, 5 as well for this script to work for those choices.


Code:
#!/bin/bash
#Filename: Assigntest Author: Luke Francis
quit=n
while [ "$quit" = "n" ]
do
  clear
  echo "OPERATOR ADMINISTRATIVE TOOL"
  echo "1. User Information"
  echo "2. Network Connectivity"
  echo "3. Processes"
  echo "4. System Information"
  echo "5. Hardware Utilization"
  echo "Q. Quit"
  echo
  echo "Which option do you require?"
  read menunumber
  case $menunumber in
       1)clear
         echo "USER INFORMATION"
         echo "1. Registered Users"
         echo "2. Disk Usage"
         echo "3. Last Logins"
         echo "4. Users Currently Logged In"
         echo "5. Total number of users"
         echo "Q. Quit"
         echo "Which option do you require?"
         read menunumber2
         case $menunumber2 in
              1)clear
                echo "The users registered on the system are:"
                echo
                awk -F: '{print $1}' /etc/passwd
                echo
                echo "Hit the Enter Key to continue"
                read junk;;
              2)clear
                echo "Disk Usage is as follows:"
                echo
                du
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              3)clear
                echo "Information on last noted login can be found next to each username."
                echo
                last
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              4)clear
                echo "Users currently logged in are:"
                echo
                w
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              5)clear
                echo "The total number of users are:"
                echo
                who -q
                echo
                echo "Hit Enter Key to continue"
                read junk;;
              Q|q)clear
                  echo "Are you sure you want to quit? Y/N"
                  read choice1
                  case $choice1 in
                       N|n)clear
                           echo "Hit Enter Key to continue"
                           read junk;;
                       Y|y)quit=y;;
                         *)clear
                       sleep 1;;
                  esac
                  ;;
         esac
         ;;
       2)clear
         echo "NETWORK CONNECTIVITY"
         echo
         echo "1. NIC Status"
         echo "2. Machine Availability"
         echo
         echo "Which option do you require?"
         read menunumber4
         case $menunumber4 in
              1)clear
                echo "Information reagrding NIC status can be found below:"
                echo
                /sbin/ifconfig
                echo
                echo "Hit the Enter Key to continue"
                read junk;;
              2)clear
                echo "Available hosts and addresses are shown below:"
                echo
                cat /etc/hosts
                echo
                echo "Hit Enter Key to continue"
                read junk;;
         esac
         ;;
       Q|q)clear
           echo "Are you sure you want to quit? Y/N"
           read choice2
           case $choice2 in
                N|n)clear
                    echo "Hit Enter key to continue"
                    read junk;;
                Y|y)quit=y;;
                  *)clear
                sleep 2;;
           esac
           ;;
  esac
done
clear
echo "Thank you for using the Operator Administrative Tool"

Hope that helps,
tyler_durden

____________________________________________________
"Only after disaster can we be resurrected."
  #7 (permalink)  
Old 04-26-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Quote:
Originally Posted by warlock129 View Post
Sorry i do not quite understand your answer if possible could you please ammend it in the code i posted earlier so i can get a better idea for what you mean. I really appreciate this.
Check your code where you have embedded the case statements and make sure that your case statements follow the correct syntax. Your error seems to be an issue with the syntax. It appears to me that you have missed to put ;; in some of your case statements.

correct the syntax and run the script again.

cheers,
Devaraj Takhellambam
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0