read command in shell script

hi all,

please read my sample script for move files
script will ask user to start movement(read command) if he does't then it sholud clear the screen and run script in while
if i do not give any input for 5 sconds then it sholud clear the script and as on

while true
do
if test [ "$yes_no" = 'Y' -o "$yes_no" = 'y' -o "$yes_no" = 'N' -o "$yes_no" = 'n' ]
then
if test $yes_no = Y -o $yes_no = y
then
echo movement started
fi
if test $yes_no = N -o $yes_no = n
then
echo movement stopped
fi
fi
echo movement is not running
echo " DO you want to continue for movement [ Y/y or N/n ]: \c "
read yes_no
echo $yes_no
sleep 5
clear
done

is this posible

read yes_no|sleep 5

sleep 5 | read yse_no

Hi.

The shell that you are using (e.g., bash, ksh, zsh) may have an option for the read built-in that allows you to set a timing requirement. What does your man page say? ... cheers, drl

There is -t option with read which wait for that much seconds and then proceeds.
In your case you can use......

read -t 5

-$nilesh

hi nilesh,

thnx for u r reply. but u r code is not working.

read -t 5 yes_no
sh: read: A specified flag is not valid for this command.

please check..

You are running 'test test'; [ is the same command as 'test'.

See below for a beter way.

while read yes_no
do
  case $yes_no in
    Y|y) echo "Movement started" ;;
    N|n) echo "Movement stopped" ;;
    * ) ;; ## Add whatever other tests you need
  esac

[/quote]

If you are using bash, there is a -t option to read.

Otherwise, use 'stty' and 'dd':

## USAGE: get_key VAR [SECS]
## SECS defaults to 5
get_key()
{
  if [ -t 0 ]
  then
    _STTY=`stty -g`
    stty -icanon min 0 time ${2:-$(( 10 * ${2:-5}))} -echo 
  fi
  _KEY=`dd bs=1 count=1 2>/dev/null`
  [ -n "$_STTY" ] && stty "$_STTY"
  [ -n "$1" ] && eval "$1=\$_KEY"
  return 0
}

Hi,

above code is working fine!

im using this code in script. whenever i press ctrl C during script execution stty setting will change and i have to logout the seesion. please give any aternative to overcome this.

If i want to increase sleep time from 5 seconds to 180 seconds then what would be the code

At the prompt, type:

stty sane

...and press <ENTER> a couple of times.

Better is to add a trap to the top of your script:

trap 'stty sane' EXIT

Note the usage of get_key:

## USAGE: get_key VAR [SECS]

hi,

its getting sleep for only 18 seconds not 180 seconds. please help

$cat get.sh
## USAGE: get_key VAR [SECS]
## SECS defaults to 5
get_key()
{
if [ -t 0 ]
then
_STTY=`stty -g`
stty -icanon min 0 time ${2:-$(( 10 * ${2:-5}))} -echo
fi
_KEY=`dd bs=1 count=1 2>/dev/null`
[ -n "$_STTY" ] && stty "$_STTY"
[ -n "$1" ] && eval "$1=\$_KEY"
return 0
}

get_key VAR 180
echo $VAR

$date;sh get.sh ;date
Thu Feb 19 10:23:32 IST 2009

Thu Feb 19 10:23:50 IST 2009
$

Sorry; that should be:

stty -icanon min 0 time $(( 10 * ${2:-5})) -echo

now it is comming with in second.

$date;sh get.sh ;date
Sun Feb 22 22:54:26 IST 2009

Sun Feb 22 22:54:27 IST 2009
$

I just discovered that the maximum value for the argument to time is 255.

To use something larger, put it in a loop with a smaller value.