exit script called from within a menu without exiting the menu












4














I have written this menu that calls forth several scripts. One of this script is



dbus-monitor --system


so it displays live traffic over dbus.



but when I want to exit this I normally do Ctrl+C, but that also exits my menu and I would like to just return to my menu.



is there a code that I can put after the dbus-moniter, when an exit is detected it starts my menu again?
my menu is just another .sh script



or ....



---------------- clarify ---------------



i am not that advanced "yet" ;) in scripting. this is the menu where i call my dbus script



select opt in "dbus Live Traffic" "option 2" "Main menu" "Quit"
do
case $opt in
"dbus Live Traffic")
curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder/dbuslivetraffic.sh | bash ;;
"option 2")
do_something ;;
"Main menu")
main_menu;;
"Quit")
quit_menu;;
esac
if [[ $opt != "Main menu" ]] || [[ $opt != "Quit" ]] ;
then
main_menu
fi
done


and this is the content of my dbuslivetraffic.sh



dbus-monitor --system


for now just this single line, but maybe in the near future more code will be added to this script.



i don't really understand where i need to put the TRAP function like suggested by @RoVo










share|improve this question





























    4














    I have written this menu that calls forth several scripts. One of this script is



    dbus-monitor --system


    so it displays live traffic over dbus.



    but when I want to exit this I normally do Ctrl+C, but that also exits my menu and I would like to just return to my menu.



    is there a code that I can put after the dbus-moniter, when an exit is detected it starts my menu again?
    my menu is just another .sh script



    or ....



    ---------------- clarify ---------------



    i am not that advanced "yet" ;) in scripting. this is the menu where i call my dbus script



    select opt in "dbus Live Traffic" "option 2" "Main menu" "Quit"
    do
    case $opt in
    "dbus Live Traffic")
    curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder/dbuslivetraffic.sh | bash ;;
    "option 2")
    do_something ;;
    "Main menu")
    main_menu;;
    "Quit")
    quit_menu;;
    esac
    if [[ $opt != "Main menu" ]] || [[ $opt != "Quit" ]] ;
    then
    main_menu
    fi
    done


    and this is the content of my dbuslivetraffic.sh



    dbus-monitor --system


    for now just this single line, but maybe in the near future more code will be added to this script.



    i don't really understand where i need to put the TRAP function like suggested by @RoVo










    share|improve this question



























      4












      4








      4


      1





      I have written this menu that calls forth several scripts. One of this script is



      dbus-monitor --system


      so it displays live traffic over dbus.



      but when I want to exit this I normally do Ctrl+C, but that also exits my menu and I would like to just return to my menu.



      is there a code that I can put after the dbus-moniter, when an exit is detected it starts my menu again?
      my menu is just another .sh script



      or ....



      ---------------- clarify ---------------



      i am not that advanced "yet" ;) in scripting. this is the menu where i call my dbus script



      select opt in "dbus Live Traffic" "option 2" "Main menu" "Quit"
      do
      case $opt in
      "dbus Live Traffic")
      curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder/dbuslivetraffic.sh | bash ;;
      "option 2")
      do_something ;;
      "Main menu")
      main_menu;;
      "Quit")
      quit_menu;;
      esac
      if [[ $opt != "Main menu" ]] || [[ $opt != "Quit" ]] ;
      then
      main_menu
      fi
      done


      and this is the content of my dbuslivetraffic.sh



      dbus-monitor --system


      for now just this single line, but maybe in the near future more code will be added to this script.



      i don't really understand where i need to put the TRAP function like suggested by @RoVo










      share|improve this question















      I have written this menu that calls forth several scripts. One of this script is



      dbus-monitor --system


      so it displays live traffic over dbus.



      but when I want to exit this I normally do Ctrl+C, but that also exits my menu and I would like to just return to my menu.



      is there a code that I can put after the dbus-moniter, when an exit is detected it starts my menu again?
      my menu is just another .sh script



      or ....



      ---------------- clarify ---------------



      i am not that advanced "yet" ;) in scripting. this is the menu where i call my dbus script



      select opt in "dbus Live Traffic" "option 2" "Main menu" "Quit"
      do
      case $opt in
      "dbus Live Traffic")
      curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder/dbuslivetraffic.sh | bash ;;
      "option 2")
      do_something ;;
      "Main menu")
      main_menu;;
      "Quit")
      quit_menu;;
      esac
      if [[ $opt != "Main menu" ]] || [[ $opt != "Quit" ]] ;
      then
      main_menu
      fi
      done


      and this is the content of my dbuslivetraffic.sh



      dbus-monitor --system


      for now just this single line, but maybe in the near future more code will be added to this script.



      i don't really understand where i need to put the TRAP function like suggested by @RoVo







      menu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 21 at 10:07

























      asked Dec 21 at 7:55









      WingZero

      385




      385






















          2 Answers
          2






          active

          oldest

          votes


















          4














          Graphical desktop environment



          You can run the command in another terminal window (if you have a graphical desktop environment).



          The following shellscript uses xterm, which can be installed with



          sudo apt update
          sudo apt install xterm


          but you can use other terminal window emulators too, for example gnome-terminal or lxterminal.



          Shellscript:



          #!/bin/bash

          select opt in "dbus-monitor --system" htop exit; do
          case $REPLY in
          1)
          xterm -e dbus-monitor --system 2>/dev/null
          ;;
          2)
          htop
          ;;
          3)
          exit
          ;;
          esac
          done


          Text screen (this method works also in a graphical desktop)



          You can use the trap method in @RoVo's answer.



          The important thing is to run the trap command before you run the command, that you must interrupt with ctrl+c.




          • So if you want it in the whole menu script, put it in the beginning.



          • If you want it only when it is absolutely necessary, run a subshell and put the trap command inside the subshell as illustrated by @RoVo, alternatively with



            bash -c 'trap "kill -SIGINT 0" SIGINT; dbus-monitor --system';;



          Shellscript:



          #!/bin/bash

          echo "Press the Enter key to print out the menu again"

          trap "kill -SIGINT 0" SIGINT

          select opt in "dbus-monitor --system" "option 2" "Quit"
          do
          case $opt in
          "dbus-monitor --system")
          dbus-monitor --system;;
          "option 2")
          echo "Hello World";;
          "Quit")
          exit;;
          esac
          done


          Comment



          Your curl command line did not work for me, so I invoke my local dbus-monitor to test that the shellscript works when using ctrl+c.






          share|improve this answer























          • i dont have a graphical desktop environment. and i cant install other shells so i am limited in my actions
            – WingZero
            Dec 21 at 10:00










          • @WingZero, in that case you cannot use the method in this answer. Is RoVo's method with a trap working for you?
            – sudodus
            Dec 21 at 10:03












          • hey, i just update my initial question, because the comment section was to small
            – WingZero
            Dec 21 at 10:07










          • problem is i am a novice in shell scripting, so i don't know where the trap method from @RoVo needs to fit in
            – WingZero
            Dec 21 at 10:08










          • @WingZero, I added an alternative with the trap method to my answer. Try that shellscript and edit it to make it do what you want.
            – sudodus
            Dec 21 at 10:46



















          5














          You can run the command in a subshell and trap on SIGINT running kill 0 to kill the process group of the subshell only.



          select opt in a b; do
          case $REPLY in
          1)
          (
          trap "kill -SIGINT 0" SIGINT
          sleep 10
          )
          ;;
          2)
          sleep 10
          ;;
          esac
          done



          • Selecting (1) will let you use Ctrl+c without killing the menu.

          • Selecting (2) and pressing Ctrl+c will kill the menu, too.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490277%2fexit-script-called-from-within-a-menu-without-exiting-the-menu%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            Graphical desktop environment



            You can run the command in another terminal window (if you have a graphical desktop environment).



            The following shellscript uses xterm, which can be installed with



            sudo apt update
            sudo apt install xterm


            but you can use other terminal window emulators too, for example gnome-terminal or lxterminal.



            Shellscript:



            #!/bin/bash

            select opt in "dbus-monitor --system" htop exit; do
            case $REPLY in
            1)
            xterm -e dbus-monitor --system 2>/dev/null
            ;;
            2)
            htop
            ;;
            3)
            exit
            ;;
            esac
            done


            Text screen (this method works also in a graphical desktop)



            You can use the trap method in @RoVo's answer.



            The important thing is to run the trap command before you run the command, that you must interrupt with ctrl+c.




            • So if you want it in the whole menu script, put it in the beginning.



            • If you want it only when it is absolutely necessary, run a subshell and put the trap command inside the subshell as illustrated by @RoVo, alternatively with



              bash -c 'trap "kill -SIGINT 0" SIGINT; dbus-monitor --system';;



            Shellscript:



            #!/bin/bash

            echo "Press the Enter key to print out the menu again"

            trap "kill -SIGINT 0" SIGINT

            select opt in "dbus-monitor --system" "option 2" "Quit"
            do
            case $opt in
            "dbus-monitor --system")
            dbus-monitor --system;;
            "option 2")
            echo "Hello World";;
            "Quit")
            exit;;
            esac
            done


            Comment



            Your curl command line did not work for me, so I invoke my local dbus-monitor to test that the shellscript works when using ctrl+c.






            share|improve this answer























            • i dont have a graphical desktop environment. and i cant install other shells so i am limited in my actions
              – WingZero
              Dec 21 at 10:00










            • @WingZero, in that case you cannot use the method in this answer. Is RoVo's method with a trap working for you?
              – sudodus
              Dec 21 at 10:03












            • hey, i just update my initial question, because the comment section was to small
              – WingZero
              Dec 21 at 10:07










            • problem is i am a novice in shell scripting, so i don't know where the trap method from @RoVo needs to fit in
              – WingZero
              Dec 21 at 10:08










            • @WingZero, I added an alternative with the trap method to my answer. Try that shellscript and edit it to make it do what you want.
              – sudodus
              Dec 21 at 10:46
















            4














            Graphical desktop environment



            You can run the command in another terminal window (if you have a graphical desktop environment).



            The following shellscript uses xterm, which can be installed with



            sudo apt update
            sudo apt install xterm


            but you can use other terminal window emulators too, for example gnome-terminal or lxterminal.



            Shellscript:



            #!/bin/bash

            select opt in "dbus-monitor --system" htop exit; do
            case $REPLY in
            1)
            xterm -e dbus-monitor --system 2>/dev/null
            ;;
            2)
            htop
            ;;
            3)
            exit
            ;;
            esac
            done


            Text screen (this method works also in a graphical desktop)



            You can use the trap method in @RoVo's answer.



            The important thing is to run the trap command before you run the command, that you must interrupt with ctrl+c.




            • So if you want it in the whole menu script, put it in the beginning.



            • If you want it only when it is absolutely necessary, run a subshell and put the trap command inside the subshell as illustrated by @RoVo, alternatively with



              bash -c 'trap "kill -SIGINT 0" SIGINT; dbus-monitor --system';;



            Shellscript:



            #!/bin/bash

            echo "Press the Enter key to print out the menu again"

            trap "kill -SIGINT 0" SIGINT

            select opt in "dbus-monitor --system" "option 2" "Quit"
            do
            case $opt in
            "dbus-monitor --system")
            dbus-monitor --system;;
            "option 2")
            echo "Hello World";;
            "Quit")
            exit;;
            esac
            done


            Comment



            Your curl command line did not work for me, so I invoke my local dbus-monitor to test that the shellscript works when using ctrl+c.






            share|improve this answer























            • i dont have a graphical desktop environment. and i cant install other shells so i am limited in my actions
              – WingZero
              Dec 21 at 10:00










            • @WingZero, in that case you cannot use the method in this answer. Is RoVo's method with a trap working for you?
              – sudodus
              Dec 21 at 10:03












            • hey, i just update my initial question, because the comment section was to small
              – WingZero
              Dec 21 at 10:07










            • problem is i am a novice in shell scripting, so i don't know where the trap method from @RoVo needs to fit in
              – WingZero
              Dec 21 at 10:08










            • @WingZero, I added an alternative with the trap method to my answer. Try that shellscript and edit it to make it do what you want.
              – sudodus
              Dec 21 at 10:46














            4












            4








            4






            Graphical desktop environment



            You can run the command in another terminal window (if you have a graphical desktop environment).



            The following shellscript uses xterm, which can be installed with



            sudo apt update
            sudo apt install xterm


            but you can use other terminal window emulators too, for example gnome-terminal or lxterminal.



            Shellscript:



            #!/bin/bash

            select opt in "dbus-monitor --system" htop exit; do
            case $REPLY in
            1)
            xterm -e dbus-monitor --system 2>/dev/null
            ;;
            2)
            htop
            ;;
            3)
            exit
            ;;
            esac
            done


            Text screen (this method works also in a graphical desktop)



            You can use the trap method in @RoVo's answer.



            The important thing is to run the trap command before you run the command, that you must interrupt with ctrl+c.




            • So if you want it in the whole menu script, put it in the beginning.



            • If you want it only when it is absolutely necessary, run a subshell and put the trap command inside the subshell as illustrated by @RoVo, alternatively with



              bash -c 'trap "kill -SIGINT 0" SIGINT; dbus-monitor --system';;



            Shellscript:



            #!/bin/bash

            echo "Press the Enter key to print out the menu again"

            trap "kill -SIGINT 0" SIGINT

            select opt in "dbus-monitor --system" "option 2" "Quit"
            do
            case $opt in
            "dbus-monitor --system")
            dbus-monitor --system;;
            "option 2")
            echo "Hello World";;
            "Quit")
            exit;;
            esac
            done


            Comment



            Your curl command line did not work for me, so I invoke my local dbus-monitor to test that the shellscript works when using ctrl+c.






            share|improve this answer














            Graphical desktop environment



            You can run the command in another terminal window (if you have a graphical desktop environment).



            The following shellscript uses xterm, which can be installed with



            sudo apt update
            sudo apt install xterm


            but you can use other terminal window emulators too, for example gnome-terminal or lxterminal.



            Shellscript:



            #!/bin/bash

            select opt in "dbus-monitor --system" htop exit; do
            case $REPLY in
            1)
            xterm -e dbus-monitor --system 2>/dev/null
            ;;
            2)
            htop
            ;;
            3)
            exit
            ;;
            esac
            done


            Text screen (this method works also in a graphical desktop)



            You can use the trap method in @RoVo's answer.



            The important thing is to run the trap command before you run the command, that you must interrupt with ctrl+c.




            • So if you want it in the whole menu script, put it in the beginning.



            • If you want it only when it is absolutely necessary, run a subshell and put the trap command inside the subshell as illustrated by @RoVo, alternatively with



              bash -c 'trap "kill -SIGINT 0" SIGINT; dbus-monitor --system';;



            Shellscript:



            #!/bin/bash

            echo "Press the Enter key to print out the menu again"

            trap "kill -SIGINT 0" SIGINT

            select opt in "dbus-monitor --system" "option 2" "Quit"
            do
            case $opt in
            "dbus-monitor --system")
            dbus-monitor --system;;
            "option 2")
            echo "Hello World";;
            "Quit")
            exit;;
            esac
            done


            Comment



            Your curl command line did not work for me, so I invoke my local dbus-monitor to test that the shellscript works when using ctrl+c.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 21 at 14:09

























            answered Dec 21 at 9:40









            sudodus

            96116




            96116












            • i dont have a graphical desktop environment. and i cant install other shells so i am limited in my actions
              – WingZero
              Dec 21 at 10:00










            • @WingZero, in that case you cannot use the method in this answer. Is RoVo's method with a trap working for you?
              – sudodus
              Dec 21 at 10:03












            • hey, i just update my initial question, because the comment section was to small
              – WingZero
              Dec 21 at 10:07










            • problem is i am a novice in shell scripting, so i don't know where the trap method from @RoVo needs to fit in
              – WingZero
              Dec 21 at 10:08










            • @WingZero, I added an alternative with the trap method to my answer. Try that shellscript and edit it to make it do what you want.
              – sudodus
              Dec 21 at 10:46


















            • i dont have a graphical desktop environment. and i cant install other shells so i am limited in my actions
              – WingZero
              Dec 21 at 10:00










            • @WingZero, in that case you cannot use the method in this answer. Is RoVo's method with a trap working for you?
              – sudodus
              Dec 21 at 10:03












            • hey, i just update my initial question, because the comment section was to small
              – WingZero
              Dec 21 at 10:07










            • problem is i am a novice in shell scripting, so i don't know where the trap method from @RoVo needs to fit in
              – WingZero
              Dec 21 at 10:08










            • @WingZero, I added an alternative with the trap method to my answer. Try that shellscript and edit it to make it do what you want.
              – sudodus
              Dec 21 at 10:46
















            i dont have a graphical desktop environment. and i cant install other shells so i am limited in my actions
            – WingZero
            Dec 21 at 10:00




            i dont have a graphical desktop environment. and i cant install other shells so i am limited in my actions
            – WingZero
            Dec 21 at 10:00












            @WingZero, in that case you cannot use the method in this answer. Is RoVo's method with a trap working for you?
            – sudodus
            Dec 21 at 10:03






            @WingZero, in that case you cannot use the method in this answer. Is RoVo's method with a trap working for you?
            – sudodus
            Dec 21 at 10:03














            hey, i just update my initial question, because the comment section was to small
            – WingZero
            Dec 21 at 10:07




            hey, i just update my initial question, because the comment section was to small
            – WingZero
            Dec 21 at 10:07












            problem is i am a novice in shell scripting, so i don't know where the trap method from @RoVo needs to fit in
            – WingZero
            Dec 21 at 10:08




            problem is i am a novice in shell scripting, so i don't know where the trap method from @RoVo needs to fit in
            – WingZero
            Dec 21 at 10:08












            @WingZero, I added an alternative with the trap method to my answer. Try that shellscript and edit it to make it do what you want.
            – sudodus
            Dec 21 at 10:46




            @WingZero, I added an alternative with the trap method to my answer. Try that shellscript and edit it to make it do what you want.
            – sudodus
            Dec 21 at 10:46













            5














            You can run the command in a subshell and trap on SIGINT running kill 0 to kill the process group of the subshell only.



            select opt in a b; do
            case $REPLY in
            1)
            (
            trap "kill -SIGINT 0" SIGINT
            sleep 10
            )
            ;;
            2)
            sleep 10
            ;;
            esac
            done



            • Selecting (1) will let you use Ctrl+c without killing the menu.

            • Selecting (2) and pressing Ctrl+c will kill the menu, too.






            share|improve this answer




























              5














              You can run the command in a subshell and trap on SIGINT running kill 0 to kill the process group of the subshell only.



              select opt in a b; do
              case $REPLY in
              1)
              (
              trap "kill -SIGINT 0" SIGINT
              sleep 10
              )
              ;;
              2)
              sleep 10
              ;;
              esac
              done



              • Selecting (1) will let you use Ctrl+c without killing the menu.

              • Selecting (2) and pressing Ctrl+c will kill the menu, too.






              share|improve this answer


























                5












                5








                5






                You can run the command in a subshell and trap on SIGINT running kill 0 to kill the process group of the subshell only.



                select opt in a b; do
                case $REPLY in
                1)
                (
                trap "kill -SIGINT 0" SIGINT
                sleep 10
                )
                ;;
                2)
                sleep 10
                ;;
                esac
                done



                • Selecting (1) will let you use Ctrl+c without killing the menu.

                • Selecting (2) and pressing Ctrl+c will kill the menu, too.






                share|improve this answer














                You can run the command in a subshell and trap on SIGINT running kill 0 to kill the process group of the subshell only.



                select opt in a b; do
                case $REPLY in
                1)
                (
                trap "kill -SIGINT 0" SIGINT
                sleep 10
                )
                ;;
                2)
                sleep 10
                ;;
                esac
                done



                • Selecting (1) will let you use Ctrl+c without killing the menu.

                • Selecting (2) and pressing Ctrl+c will kill the menu, too.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 21 at 9:05

























                answered Dec 21 at 8:37









                RoVo

                2,558215




                2,558215






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490277%2fexit-script-called-from-within-a-menu-without-exiting-the-menu%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]