read command in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read command in shell script
# 1  
Old 08-20-2008
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
# 2  
Old 08-20-2008
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
# 3  
Old 08-20-2008
There is -t option with read which wait for that much seconds and then proceeds.
In your case you can use......
Code:
read -t 5

-$nilesh
# 4  
Old 08-24-2008
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..
# 5  
Old 08-24-2008
Quote:
Originally Posted by arvindng
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' ]

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

See below for a beter way.
Quote:
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

Code:
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:

is this posible

read yes_no|sleep 5

sleep 5 | read yse_no
[/QUOTE]

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

Otherwise, use 'stty' and 'dd':

Code:
## 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
}

# 6  
Old 02-18-2009
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
# 7  
Old 02-18-2009
Quote:
Originally Posted by arvindng
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.

At the prompt, type:

Code:
stty sane

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

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

Code:
trap 'stty sane' EXIT

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

Note the usage of get_key:

Code:
## USAGE: get_key VAR [SECS]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell read command is not waiting for user input

Hi, i am working on one automation , for that i have writing one shell program that take user input in "while read line" block. but read command is taking value that is readed by While block. while read line; do command 1; command 2 echo -n "Do you want to continute > " read rsp... (2 Replies)
Discussion started by: ranvijaidba
2 Replies

2. Shell Programming and Scripting

Read input in shell script

I want to write a script that read the input to variable. I want the input screen to have 2 lines, where the values already input will appear above the input line for example if I want to input the words below: like love live life The screen will display like this: 1. Before any input... (8 Replies)
Discussion started by: aydj
8 Replies

3. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. Shell Programming and Scripting

How to read a file through shell script?

hi all, i have to read a file using shell script for example my csv file is like this Tid Inputfille Inputfilepath 1 ABC_20141218.idr /export/home/him60t1/input 2 ABC_20141219.idr /export/home/him60t1/input1 what i have to do is if on my command line i... (1 Reply)
Discussion started by: ramsavi
1 Replies

6. Shell Programming and Scripting

Read webpage from shell script

Hey experts, I am trying to read a webpage and want to search for a text patter (case insensitive). Please help me acquire this. If the text is found i will receive a mail which can be hardcoded in the script. I am not a big fan of using PERL script. Please help. Machine: AIX... (15 Replies)
Discussion started by: bankimmehta
15 Replies

7. Shell Programming and Scripting

How can I send the input of a read line command through a shell script

Hi All, I wish to automate the unix command 'su' through a shell script. I would like to pass the content of a file as password to 'su' command. My script is as below, #! /bin/sh su userA while read line do rpm -ivh $line done < pwd.txt where pwd.txt contains the password of... (6 Replies)
Discussion started by: little_wonder
6 Replies

8. Programming

How to read output of a shell command

Hello All, I have a an application written in C and runing on Red Hat Linux. In my code I have written a command that is fired on the linux shell by using system() function call. Now I need to read the output of this command in my c program and assign it to a variable. Can anyone... (1 Reply)
Discussion started by: shamik
1 Replies

9. Shell Programming and Scripting

Perl script variable to read shell command

Solaris 10 Korn shell ksh, Hi there, I have figured out to get yesterday's date which is using the below command: TZ=GMT+24; date +%d-%b-%Y to get the format of 30-Sep-2008 and TZ=GMT+24; date +%Y%m%d to get the format of 20080930. I need this two format. In my perl script below I need... (4 Replies)
Discussion started by: bulkbiz
4 Replies

10. Shell Programming and Scripting

the shell not pause when execute read command

Hi, i facing a problem when run the script below.. while do if then printf "Please enter a name : " read response # the problem occur here if then ea_ident=${omc_ident} else # # Check that name does not contain invalid... (14 Replies)
Discussion started by: neruppu
14 Replies
Login or Register to Ask a Question