LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-24-2010, 08:38 AM   #1
bull81
LQ Newbie
 
Registered: Jan 2010
Posts: 5

Rep: Reputation: 0
Unhappy bash ctrl+c tarp and bash read with timeout !?!


Hi all

I always find answers on this forum but this problem is just wrecking my head!

Please check this simple bash code:

Code:
#!/bin/bash
trap "echo 'you got me'" SIGINT SIGTERM        # to trap ctrl+c
echo "Press ctrl+c during 5 sec loop"
for ((i=0;i<5;i++)); do
        echo $i
        sleep 1
done

read -t 1 -s tmp_with_timeout
echo "after timeout read, now press again ctrl+c during 5 sec loop"

for ((i=0;i<5;i++)); do
        echo $i
        sleep 1
done

echo "end"
Output looks like this:
Code:
Press ctrl+c during 5 sec loop
0
1
you got me
2
3
4
after timeout read, now press again ctrl+c during 5 sec loop
0
1
2
you got me
end
And my question: How come code behaves normally and stops when ctrl+c signal is caught and resumes, but after I use at least one timeout read in the code it looks like, if signal is caught again it doesn't pause the execution but skips the loop.
If you remove -t (timeout) option from the read, both loops look the same!

Example is very simple to demonstrate the problem. It took me few hours to finally get it from over 2500 lines of code and guys you're my last hope!

Please help!

Last edited by bull81; 01-24-2010 at 08:49 AM.
 
Old 01-24-2010, 06:05 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Try changing the line:
Code:
trap "echo 'you got me'" SIGINT SIGTERM        # to trap ctrl+c

to:
Code:
trap "echo 'you got me'; exit" SIGINT SIGTERM        # to trap ctrl+c
or if you need expand functionality:
Code:
function mytrap
{
echo 'you got me'
exit
}

trap mytrap SIGINT SIGTERM        # to trap ctrl+c
hth
 
Old 01-25-2010, 05:44 AM   #3
bull81
LQ Newbie
 
Registered: Jan 2010
Posts: 5

Original Poster
Rep: Reputation: 0
kbp: Thanks for the repla

If I just want to control exit all is good, but what I want to do is after ctrl +c ask user do you want to exit [yes/no]:
If user say no and I want to continue with script, I can't because as you can see from second loop, script doesn't pause on ctrl+c signal after timeout read. Everything works perfect till I use timeout read. For some reason after that read command script don't pause anymore on signals.
I am not that good with strace so I was hoping that one of the programming gurus can explain that to me.
 
Old 01-25-2010, 08:46 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The lm-sensors' fancontrol script's main loop may hold the solution to this issue:
Code:
# main loop calling the main function at specified intervals
while true
do
    UpdateFanSpeeds
    # Sleep while still handling signals
    sleep $INTERVAL &
    wait $!
done
 
Old 01-25-2010, 08:46 AM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Here's an updated function:

Code:
function mytrap
{
echo 'you got me'
read -p 'would you like to exit ? y/n ' ANS
if [ "$ANS" = "y" ]
then
exit
fi
}
.. seemed to test ok

Issue with timeout still occurs though... strange one

I was just reading the man page... the error may be occurring because 'read -t 1 ... ' actually returns with an error code > 128 if the timout is exceeded, I replaced the 'read -t 1' call with a 'sleep 1' and the handler works as expected

Code:
#!/bin/bash

function mytrap
{
echo 'you got me'
read -p 'would you like to exit ? y/n ' ANS
if [ "$ANS" = "y" ]
then
exit
else
return 0
fi
}

trap mytrap SIGINT SIGTERM        # to trap ctrl+c
echo "Press ctrl+c during 5 sec loop"
for ((i=0;i<5;i++)); do
        echo $i
        sleep 1
done

#read -t 1 -s tmp_with_timeout
sleep 1
echo "after timeout read, now press again ctrl+c during 5 sec loop"

for ((i=0;i<5;i++)); do
        echo $i
        sleep 1
done

echo "end"
cheers

Last edited by kbp; 01-25-2010 at 09:11 AM.
 
Old 01-25-2010, 08:55 AM   #6
bull81
LQ Newbie
 
Registered: Jan 2010
Posts: 5

Original Poster
Rep: Reputation: 0
kbp:
Exactly what I have in my code! Now, with your function, try to ctrl+c in first loop answer n and then in the second loop and answer n! See the difference?! In first loop it pause and then resume after you answer n, in second loop, after timeout read it just exit the loop even if you don't answer 'y' to exit! ITS THE SAME CODE and yet it behaves differently if I use read with -t! I have no idea why!

For those who wants to try:
Code:
#!/bin/bash
trap mytrap SIGINT SIGTERM        # to trap ctrl+c

function mytrap
{
   echo 'you got me'
   read -p 'would you like to exit ? y/n ' ANS
   if [ "$ANS" = "y" ]
   then
   exit
   fi
}

echo "Press ctrl+c during 5 sec loop"
for ((i=0;i<5;i++)); do
        echo $i
        sleep 1
done

read -t 1 -s tmp_with_timeout
echo "after timeout read, now press again ctrl+c during 5 sec loop"

for ((i=0;i<5;i++)); do
        echo $i
        sleep 1
done

Last edited by bull81; 01-25-2010 at 08:58 AM.
 
Old 01-25-2010, 09:12 AM   #7
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Sorry... I edited my last post, see above

cheers
 
Old 01-25-2010, 09:28 AM   #8
bull81
LQ Newbie
 
Registered: Jan 2010
Posts: 5

Original Poster
Rep: Reputation: 0
kbp: Oh yes I know it works, because you comment out timeout read...now in your code please uncomment
#read -t 1 -s tmp_with_timeout
to have timeout read and check again, you will see what i mean

ps. I need timeout reads in code to avoid pauses if user is not watching.
 
Old 01-25-2010, 09:32 AM   #9
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I know it fails... that's what I was saying, if 'read -t 1 ...' doesn't receive any input before the timeout period, its return code is non-zero ( greater than 128 ) .... maybe this is the reason that the traps are handled differently

cheers
 
Old 01-25-2010, 09:46 AM   #10
bull81
LQ Newbie
 
Registered: Jan 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Sorry I missed that. Let me dig a lttle....

Cheers!
 
Old 01-25-2010, 09:56 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by bull81 View Post
Sorry I missed that. Let me dig a lttle....

Cheers!
And please try the technique in this post above which may have been overlooked in the flurry of replies.
 
  


Reply



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
Ctrl + C in bash mircan Linux - General 5 12-08-2009 02:48 PM
Read and Timeout Problem bash JuanJose Linux - Newbie 7 06-12-2008 07:10 PM
Any way to create bash short cuts (like CTRL+l for clear and Ctrl+D for exit) supersubu123 Linux - General 5 05-30-2007 03:02 AM
bash script to read CTRL+D panchosansa Programming 5 10-26-2006 03:25 AM
bash ctrl+a not working! SteveGodfrey Linux - General 3 09-10-2004 07:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 PM.

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