Accepting CTRL-C as input











up vote
2
down vote

favorite












I have one problem in a Linux script.



#!/bin/bash 
x=1;
while [ $x != 0 ]
do
echo "Type a command:"
read -r command
eval "$command"
if [ "$command" = end ]
then x=0;
fi
if [ "$command" = kill ]
then echo "To end the program, type the end"
fi
done


And I want to make the same thing with CTRL_C like for kill. When a user types CTRL_C, script will display: To end the program, type the end. But I don't know how to do that.



An assignment is :



Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"










share|improve this question









New contributor




Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Ctrl+C can be handled in a trap statement, or you can arrange things so that the Ctrl-C is read as a character by the read. Which would you prefer? If this is an assignment , please give us all the details.
    – Mark Plotnick
    4 hours ago










  • Ignoring your question for the moment, should't the eval $command part be after the two ifs? You may also want to take a look at case in the Compound Commands section of the man page for bash.
    – nohillside
    4 hours ago










  • @MarkPlotnick An assignment is : Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key <ctrl-c> was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"
    – Miggu
    4 hours ago










  • @nohillside Sure, I will consider that.
    – Miggu
    4 hours ago










  • Could you please edit your question to include the text of the assignment from that comment? I think it would make it easier for people to answer.
    – Mark Plotnick
    4 hours ago

















up vote
2
down vote

favorite












I have one problem in a Linux script.



#!/bin/bash 
x=1;
while [ $x != 0 ]
do
echo "Type a command:"
read -r command
eval "$command"
if [ "$command" = end ]
then x=0;
fi
if [ "$command" = kill ]
then echo "To end the program, type the end"
fi
done


And I want to make the same thing with CTRL_C like for kill. When a user types CTRL_C, script will display: To end the program, type the end. But I don't know how to do that.



An assignment is :



Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"










share|improve this question









New contributor




Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Ctrl+C can be handled in a trap statement, or you can arrange things so that the Ctrl-C is read as a character by the read. Which would you prefer? If this is an assignment , please give us all the details.
    – Mark Plotnick
    4 hours ago










  • Ignoring your question for the moment, should't the eval $command part be after the two ifs? You may also want to take a look at case in the Compound Commands section of the man page for bash.
    – nohillside
    4 hours ago










  • @MarkPlotnick An assignment is : Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key <ctrl-c> was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"
    – Miggu
    4 hours ago










  • @nohillside Sure, I will consider that.
    – Miggu
    4 hours ago










  • Could you please edit your question to include the text of the assignment from that comment? I think it would make it easier for people to answer.
    – Mark Plotnick
    4 hours ago















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have one problem in a Linux script.



#!/bin/bash 
x=1;
while [ $x != 0 ]
do
echo "Type a command:"
read -r command
eval "$command"
if [ "$command" = end ]
then x=0;
fi
if [ "$command" = kill ]
then echo "To end the program, type the end"
fi
done


And I want to make the same thing with CTRL_C like for kill. When a user types CTRL_C, script will display: To end the program, type the end. But I don't know how to do that.



An assignment is :



Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"










share|improve this question









New contributor




Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have one problem in a Linux script.



#!/bin/bash 
x=1;
while [ $x != 0 ]
do
echo "Type a command:"
read -r command
eval "$command"
if [ "$command" = end ]
then x=0;
fi
if [ "$command" = kill ]
then echo "To end the program, type the end"
fi
done


And I want to make the same thing with CTRL_C like for kill. When a user types CTRL_C, script will display: To end the program, type the end. But I don't know how to do that.



An assignment is :



Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"







shell-script scripting






share|improve this question









New contributor




Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 22 mins ago









Rui F Ribeiro

38.6k1479128




38.6k1479128






New contributor




Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









Miggu

133




133




New contributor




Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Miggu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Ctrl+C can be handled in a trap statement, or you can arrange things so that the Ctrl-C is read as a character by the read. Which would you prefer? If this is an assignment , please give us all the details.
    – Mark Plotnick
    4 hours ago










  • Ignoring your question for the moment, should't the eval $command part be after the two ifs? You may also want to take a look at case in the Compound Commands section of the man page for bash.
    – nohillside
    4 hours ago










  • @MarkPlotnick An assignment is : Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key <ctrl-c> was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"
    – Miggu
    4 hours ago










  • @nohillside Sure, I will consider that.
    – Miggu
    4 hours ago










  • Could you please edit your question to include the text of the assignment from that comment? I think it would make it easier for people to answer.
    – Mark Plotnick
    4 hours ago




















  • Ctrl+C can be handled in a trap statement, or you can arrange things so that the Ctrl-C is read as a character by the read. Which would you prefer? If this is an assignment , please give us all the details.
    – Mark Plotnick
    4 hours ago










  • Ignoring your question for the moment, should't the eval $command part be after the two ifs? You may also want to take a look at case in the Compound Commands section of the man page for bash.
    – nohillside
    4 hours ago










  • @MarkPlotnick An assignment is : Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key <ctrl-c> was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"
    – Miggu
    4 hours ago










  • @nohillside Sure, I will consider that.
    – Miggu
    4 hours ago










  • Could you please edit your question to include the text of the assignment from that comment? I think it would make it easier for people to answer.
    – Mark Plotnick
    4 hours ago


















Ctrl+C can be handled in a trap statement, or you can arrange things so that the Ctrl-C is read as a character by the read. Which would you prefer? If this is an assignment , please give us all the details.
– Mark Plotnick
4 hours ago




Ctrl+C can be handled in a trap statement, or you can arrange things so that the Ctrl-C is read as a character by the read. Which would you prefer? If this is an assignment , please give us all the details.
– Mark Plotnick
4 hours ago












Ignoring your question for the moment, should't the eval $command part be after the two ifs? You may also want to take a look at case in the Compound Commands section of the man page for bash.
– nohillside
4 hours ago




Ignoring your question for the moment, should't the eval $command part be after the two ifs? You may also want to take a look at case in the Compound Commands section of the man page for bash.
– nohillside
4 hours ago












@MarkPlotnick An assignment is : Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key <ctrl-c> was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"
– Miggu
4 hours ago




@MarkPlotnick An assignment is : Write a script working in a loop, displaying the "Enter command:" message and retrieving a string from the keyboard. If the user types "end", the script should end. In all other cases, he should treat the entered string as a command that should be executed by the shell. If during the work the key <ctrl-c> was pressed or an attempt was made to kill the script with the kill command, the script should display the following information: To finish the work write "end"
– Miggu
4 hours ago












@nohillside Sure, I will consider that.
– Miggu
4 hours ago




@nohillside Sure, I will consider that.
– Miggu
4 hours ago












Could you please edit your question to include the text of the assignment from that comment? I think it would make it easier for people to answer.
– Mark Plotnick
4 hours ago






Could you please edit your question to include the text of the assignment from that comment? I think it would make it easier for people to answer.
– Mark Plotnick
4 hours ago












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Ctrl-C is "special" when you have terminal emulation because it is caught and results in a signal (SIGINT) to be sent to your script. You have two options:




  • You can make your script catch SIGINT and handle it as you wish. This is done with trap xxx SIGINT where xxx is a bash function to execute

  • You can make the terminal ignore ctrl-c by running stty intr undef. You'll most probably have some issues catching ctrl-c though.






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',
    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
    });


    }
    });






    Miggu is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489360%2faccepting-ctrl-c-as-input%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Ctrl-C is "special" when you have terminal emulation because it is caught and results in a signal (SIGINT) to be sent to your script. You have two options:




    • You can make your script catch SIGINT and handle it as you wish. This is done with trap xxx SIGINT where xxx is a bash function to execute

    • You can make the terminal ignore ctrl-c by running stty intr undef. You'll most probably have some issues catching ctrl-c though.






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      Ctrl-C is "special" when you have terminal emulation because it is caught and results in a signal (SIGINT) to be sent to your script. You have two options:




      • You can make your script catch SIGINT and handle it as you wish. This is done with trap xxx SIGINT where xxx is a bash function to execute

      • You can make the terminal ignore ctrl-c by running stty intr undef. You'll most probably have some issues catching ctrl-c though.






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Ctrl-C is "special" when you have terminal emulation because it is caught and results in a signal (SIGINT) to be sent to your script. You have two options:




        • You can make your script catch SIGINT and handle it as you wish. This is done with trap xxx SIGINT where xxx is a bash function to execute

        • You can make the terminal ignore ctrl-c by running stty intr undef. You'll most probably have some issues catching ctrl-c though.






        share|improve this answer












        Ctrl-C is "special" when you have terminal emulation because it is caught and results in a signal (SIGINT) to be sent to your script. You have two options:




        • You can make your script catch SIGINT and handle it as you wish. This is done with trap xxx SIGINT where xxx is a bash function to execute

        • You can make the terminal ignore ctrl-c by running stty intr undef. You'll most probably have some issues catching ctrl-c though.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 4 hours ago









        V13

        2,724613




        2,724613






















            Miggu is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Miggu is a new contributor. Be nice, and check out our Code of Conduct.













            Miggu is a new contributor. Be nice, and check out our Code of Conduct.












            Miggu is a new contributor. Be nice, and check out our Code of Conduct.
















            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%2f489360%2faccepting-ctrl-c-as-input%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

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei