Trap “break” not working while read command is being called
I have this bash-written hangman game for a school project, and currently I am trying to replace the ctrl-c exit command for a while loop break, so the user goes back from the actual game to the categories selection menu.
The code I am currently using is:
trap "break" SIGINT
and inside the while loop:
read CHAR
Everything works fine when a text is printed in the loop and also when the sleep command is running, but when the algorithm is at the read command, pressing ctrl-c just inputs "^C" on the console.
bash read trap sigint
add a comment |
I have this bash-written hangman game for a school project, and currently I am trying to replace the ctrl-c exit command for a while loop break, so the user goes back from the actual game to the categories selection menu.
The code I am currently using is:
trap "break" SIGINT
and inside the while loop:
read CHAR
Everything works fine when a text is printed in the loop and also when the sleep command is running, but when the algorithm is at the read command, pressing ctrl-c just inputs "^C" on the console.
bash read trap sigint
Please update your post to show all the steps you have taken. Include as much of the script as possible to help troubleshoot the issue.
– kemotep
Apr 3 at 2:01
add a comment |
I have this bash-written hangman game for a school project, and currently I am trying to replace the ctrl-c exit command for a while loop break, so the user goes back from the actual game to the categories selection menu.
The code I am currently using is:
trap "break" SIGINT
and inside the while loop:
read CHAR
Everything works fine when a text is printed in the loop and also when the sleep command is running, but when the algorithm is at the read command, pressing ctrl-c just inputs "^C" on the console.
bash read trap sigint
I have this bash-written hangman game for a school project, and currently I am trying to replace the ctrl-c exit command for a while loop break, so the user goes back from the actual game to the categories selection menu.
The code I am currently using is:
trap "break" SIGINT
and inside the while loop:
read CHAR
Everything works fine when a text is printed in the loop and also when the sleep command is running, but when the algorithm is at the read command, pressing ctrl-c just inputs "^C" on the console.
bash read trap sigint
bash read trap sigint
edited Dec 16 at 11:50
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Apr 3 at 0:58
Marcos Rios
62
62
Please update your post to show all the steps you have taken. Include as much of the script as possible to help troubleshoot the issue.
– kemotep
Apr 3 at 2:01
add a comment |
Please update your post to show all the steps you have taken. Include as much of the script as possible to help troubleshoot the issue.
– kemotep
Apr 3 at 2:01
Please update your post to show all the steps you have taken. Include as much of the script as possible to help troubleshoot the issue.
– kemotep
Apr 3 at 2:01
Please update your post to show all the steps you have taken. Include as much of the script as possible to help troubleshoot the issue.
– kemotep
Apr 3 at 2:01
add a comment |
1 Answer
1
active
oldest
votes
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.
That is the reason why the read
is not interrupted.
The ^C
is not printed by the shell but by the terminal due to the setting echoctl
. If you execute stty -echoctl
then it does not appear.
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',
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
});
}
});
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%2f435166%2ftrap-break-not-working-while-read-command-is-being-called%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
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.
That is the reason why the read
is not interrupted.
The ^C
is not printed by the shell but by the terminal due to the setting echoctl
. If you execute stty -echoctl
then it does not appear.
add a comment |
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.
That is the reason why the read
is not interrupted.
The ^C
is not printed by the shell but by the terminal due to the setting echoctl
. If you execute stty -echoctl
then it does not appear.
add a comment |
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.
That is the reason why the read
is not interrupted.
The ^C
is not printed by the shell but by the terminal due to the setting echoctl
. If you execute stty -echoctl
then it does not appear.
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.
That is the reason why the read
is not interrupted.
The ^C
is not printed by the shell but by the terminal due to the setting echoctl
. If you execute stty -echoctl
then it does not appear.
answered Apr 3 at 1:39
Hauke Laging
55.8k1285133
55.8k1285133
add a comment |
add a comment |
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%2f435166%2ftrap-break-not-working-while-read-command-is-being-called%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
Please update your post to show all the steps you have taken. Include as much of the script as possible to help troubleshoot the issue.
– kemotep
Apr 3 at 2:01