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"
shell-script scripting
New contributor
|
show 1 more comment
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"
shell-script scripting
New contributor
Ctrl+C can be handled in atrap
statement, or you can arrange things so that the Ctrl-C is read as a character by theread
. 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 theeval $command
part be after the twoif
s? You may also want to take a look atcase
in the Compound Commands section of the man page forbash
.
– 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
|
show 1 more comment
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"
shell-script scripting
New contributor
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
shell-script scripting
New contributor
New contributor
edited 22 mins ago
Rui F Ribeiro
38.6k1479128
38.6k1479128
New contributor
asked 4 hours ago
Miggu
133
133
New contributor
New contributor
Ctrl+C can be handled in atrap
statement, or you can arrange things so that the Ctrl-C is read as a character by theread
. 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 theeval $command
part be after the twoif
s? You may also want to take a look atcase
in the Compound Commands section of the man page forbash
.
– 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
|
show 1 more comment
Ctrl+C can be handled in atrap
statement, or you can arrange things so that the Ctrl-C is read as a character by theread
. 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 theeval $command
part be after the twoif
s? You may also want to take a look atcase
in the Compound Commands section of the man page forbash
.
– 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 if
s? 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 if
s? 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
|
show 1 more comment
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.
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
add a comment |
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.
add a comment |
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.
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.
answered 4 hours ago
V13
2,724613
2,724613
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theread
. 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 twoif
s? You may also want to take a look atcase
in the Compound Commands section of the man page forbash
.– 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