Reading from Keyboard Shell Script

Question: How do I read from kb using shell script but I need to read it from same line?

Script :-

Code:

echo "Please Enter Your Choice "
read CHOICE

But it goes to next line I need it to read it next to Choice and not new line.

Solution:

Code:

printf "Please Enter Your Choice "
read CHOICE
echo $CHOICE

or

Code:

echo -n "Please Enter Your Choice "
read CHOICE
 

Question: csh exit while loop on keystroke 

#!/bin/csh

I'm using a `while(1)` loop to dispaly real-time information about various files on my system, and I use ^C to exit it when needed. I was hoping there was a way to exit the script on a normal keystroke such as "q". 

Solution:

Use a signal and have your script listen for the signal. The syntax depends on the shell you are using. If you are using ksh, see man ksh. 

trap "command to run when signal trapped" "list of signals"

trap "" signals # Will trap the signals you define but not allow them to affect your script.

You can get a list of signals by typing kill -l on the command line. Note: You cannot trap signal 9. Your "command to run" can be a function if you so choose.

Code:

User Commands                                             trap(1)

NAME
     trap, onintr  -  shell  built-in  functions  to  respond  to
     (hardware) signals

SYNOPSIS
  sh
     trap [  argument  n  [ n2 ... ]  ]

  csh
     onintr [ -| label ]

  ksh
     *trap [  arg sig  [  sig2 ...  ]  ]

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.