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 02-26-2014, 02:11 PM   #1
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Rep: Reputation: Disabled
BASH take argument if user don't type anything


Hi everyone, i have the following script but as you can see, you need to type "y" or "n" in order to do something, is there any way for the script to send a "y" or a "n" after 10 seconds if the user don't type anything?

Code:
echo " Continue ..."
while true ; do
	read ANSWER
	case $ANSWER in
	Y|y )
#		echo " you said yes"
		date
		;;
	N|n )
#		echo " you said no"
		exit 255
		;;
	* )
		exit 1
		;;
esac
done
 
Old 02-26-2014, 02:20 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
From "man read"

Quote:
-t timeout
Cause read to time out and return failure if a complete
line of input is not read within timeout seconds. time‐
out may be a decimal number with a fractional portion
following the decimal point. This option is only effec‐
tive if read is reading input from a terminal, pipe, or
other special file; it has no effect when reading from
regular files. If timeout is 0, read returns success if
input is available on the specified file descriptor,
failure otherwise. The exit status is greater than 128
if the timeout is exceeded.
Use $? to check read's exit status and then take the necessary action.

Last edited by suicidaleggroll; 02-26-2014 at 02:21 PM.
 
Old 02-26-2014, 02:42 PM   #3
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
Great, you just gave me an idea. I will try it and post here.

Thanks again.
 
Old 02-26-2014, 03:40 PM   #4
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
This is the final script i had in mind.

Code:
#!/bin/bash

clear
COUNTER=0
echo " Continue ... [y|n]"
while true ; do
	read -t 60 ANSWER
	if [ $? -eq 0 ]; then
		case $ANSWER in
		Y|y )
			xdotool mousemove 1200 10
			sleep 3
			xdotool click 3
			xdotool mousemove 100 100
			;;
		N|n )
			exit 255
			;;
		* )
			exit 1
			;;
		esac
	else
			xdotool mousemove 1200 10
			sleep 3
			xdotool click 3
			xdotool mousemove 100 100
	fi
let COUNTER=COUNTER+1
echo $COUNTER
done
 
Old 02-26-2014, 08:16 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Your script currently only prompts the user once to continue, yet if 'Y' is chosen a blank prompt will appear again and it may not be obvious to the user that they need to enter something.

Also I am a big believer in not replicating code if possible, so you could optionally do something like:
Code:
#!/bin/bash

clear
COUNTER=0

while true ; do
	read -n 1 -p " Continue ... [y|n]" -t 60 ANSWER
	ANSWER=${ANSWER:-Y}
	
	case ${ANSWER^} in
	Y )
		xdotool mousemove 1200 10
		sleep 3
		xdotool click 3
		xdotool mousemove 100 100
		;;
	N )
		exit 255
		;;
	* )
		exit 1
		;;
	esac
	(( COUNTER++ ))
	echo $COUNTER
done
 
  


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
output from bash script as an argument?????? rehan21 Programming 8 02-09-2013 09:10 AM
(SOLVED) bash shell read user argument from command line and test it atnonis Programming 12 08-30-2010 07:11 AM
Python: can you determine the type of shell the user is using (tcsh, bash, etc)? BrianK Programming 1 01-20-2010 09:43 PM
Unable to type password after user name in BASH SHELL Nack Linux - Newbie 6 10-27-2009 11:17 PM
warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type eCli Programming 1 01-19-2009 07:44 AM

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

All times are GMT -5. The time now is 07: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