LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-03-2017, 01:17 PM   #1
fusion1275
Member
 
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 288

Rep: Reputation: 36
Trying to do nested case statement with great difficulties


Hi all,

So I am trying build a quick bash script that will stop an application on 1 level of the case statement but inside it can bring down certain components of it separately but I am really fighting with it. The commands I want on the cmd line are the following:

Quote:
./script.sh start
./script.sh start comp
./script.sh stop
./script.sh stop comp
Code I have currently:

Code:
ACTION="$1"
OPTION="$2"

        case ${ACTION} in
                        start)
                                echo "starting the application"
                                ;;
                                                
                                case ${OPTION} in
                                        comp) echo "Starting app component"
                                        ;;
                                esac

                        stop)
                                echo "stopping the application"
                                ;;

esac
Please be gently as I did try and I am very much still learning how this all works.

Thanks for any help you can provide.
 
Old 09-03-2017, 02:28 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,633

Rep: Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189Reputation: 4189
One of those can't see the forest for the trees.

Code:
case ${ACTION} in
      start)
            echo "starting the application"
             ;;  <--- misplaced
                                                
            case ${OPTION} in
                  comp) echo "Starting app component"
                  ;;
            esac
            ;;  <--- should be here.

     stop)
         echo "stopping the application"
         ;;

esac
Using set -xv at the beginning of a script will help you debug your code.
 
Old 09-04-2017, 10:33 AM   #3
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 676

Rep: Reputation: Disabled
you can also remove the static variables and put them directly in the case statement.

Code:
case "$1" in
  start)
    echo "Starting application"
    case "$2" in
      comp)
        echo "starting app component"
        ;;
    esac
    ;;
  stop)
    echo "stopping application"
    ;;
esac
 
Old 09-05-2017, 03:10 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,772

Rep: Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798
Usually I do not further indent the parts that belong to the case-esac.
Also I put symmetric ( ) that is allowed in all Posix shells, avoids a problem with $( ) in bash-3 and has advantages in text editors.
Code:
case $ACTION in
(start)
    echo "starting the application"
    case $OPTION in
    (comp)
        echo "Starting app component"
    ;;
    esac
;;
(stop)
    echo "stopping the application"
;;
esac
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
nested if statement dsmith8890 Programming 3 06-24-2012 10:45 AM
[SOLVED] BASH Question - nested case statements jbeiter Linux - Software 2 02-13-2012 10:08 AM
Nested case kasparov29 Programming 4 12-05-2011 12:21 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
system() function in nested if else/case! natalinasmpf Programming 10 01-26-2004 06:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration