LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-10-2008, 09:33 AM   #1
SlackerJack
LQ Newbie
 
Registered: Aug 2006
Distribution: Slackware 11.0
Posts: 28

Rep: Reputation: 15
invisible chrs and no newline in bash after running script with 'trap 2' and 'read'


Hi,

I have a script where I want to trap 'ctrl-c' so I can do some things before exiting.

Entering 'Q' works OK.

But when I do ^c (ctrl-c), and answering 'y', thus exiting, I get the described effects.

Something to do with 'read -s'?

Code:
#!/bin/bash

# on 'Ctrl-C', call function 'QUIT'
#trap QUIT TERM INT
trap QUIT 2

function QUIT
{
 #echo -e " \n"
 #echo -e "\033[0m"
 echo -n "Are you sure you want to quit? (y/n): "
 read ANSWER
 if [ "$ANSWER" = "y" ] ; then echo ; exit 0 ; fi
 PRINT
}

function PRINT
{
 echo "-Menu-"
 echo " 1) blabla"
 echo " 2) blablabla"
}

# main loop
while :
do
 PRINT
 #read -n1 KEY    # this works
 read -s -n1 KEY  # this does not
 
 case $KEY in
  "1" ) echo "not implemented yet" ; sleep 1 ;;
  "2" ) echo "not implemented yet" ; sleep 1 ;;
  "Q" ) QUIT ;;
   *  ) echo "unknown key:'$KEY'" ; sleep 1 ;;
 esac
done

echo "-END-"
exit 0
 
Old 08-11-2008, 05:04 AM   #2
SlackerJack
LQ Newbie
 
Registered: Aug 2006
Distribution: Slackware 11.0
Posts: 28

Original Poster
Rep: Reputation: 15
Well, tried some things and this is what I found:

Code:
function QUIT
{
 # this would solve it
 #read -n1 -p "Are you sure you want to quit? (y/n): " ANSWER

 # this would solve it
 #read -s -p "Are you sure you want to quit? (y/n): " ANSWER

 # this will NOT solve it
 read -s -t 0 -n1 ANSWER
 echo "debug1:$ANSWER"
 read -p "Are you sure you want to quit? (y/n): " ANSWER

 if [ "$ANSWER" = "y" ] ; then exit 0 ; fi
}
I suppose that is good enough to move on...
 
Old 08-11-2008, 09:37 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
When you interrupt read, the terminal is left in raw or cbreak mode. You have to reset it. Add :

stty sane

as your first statement inside your QUIT handler.
 
Old 08-11-2008, 11:03 AM   #4
SlackerJack
LQ Newbie
 
Registered: Aug 2006
Distribution: Slackware 11.0
Posts: 28

Original Poster
Rep: Reputation: 15
Ah, there it is.

Thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newline in bash Srinivas B T Linux - General 2 06-11-2007 05:14 AM
bash script to read CTRL+D panchosansa Programming 5 10-26-2006 03:25 AM
read write in bash script yhus Programming 6 09-18-2006 12:23 PM
Bash Script for a newline olefemmy Programming 1 11-11-2004 11:16 PM
Help with BASH script PLEASE READ!!! hroman Programming 7 10-08-2004 07:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:43 AM.

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