How to introduce delay in Shell Script till a key stroke.

Hi All,

How to introduce delay in the Bash/Shell Script till key stroke.
Below is the requirement..

1.Execute set of commands.
2.Display the message echo "press any key to continue"
Introduce a delay till key stroke.
3.Execute next set of commands.

Thanks in Advance

Sunil.K

Misunderstood, nvm.

Hi.

Perhaps this link will help?

1 Like

Or if your shell supports this option:

read -t

I wrote a function to handle different cases of pause in shell scripts. You can simplify it for your needs.

pause()	{	# [1:timeout] [2:prompt]
	local P T
	[[ $1 =~ ^[0-9]+$ ]] && { T=$1; shift; }
	P=${1:-Hit any key...}
     echo -n "$P " >&2
     if ((T))
     then	for ((P=$T; P>0; P--))
		do	read -sn 1 -p "($P) " -t 1 && break
			tput cub $((${#P}+3))
		done
	else
		read -sn 1
	fi
	echo >&2
	return
}

Default prompt is given if none provided.
The plus is that it displays a countdown.
You can call it like

pause 10

to pause 10 seconds
if no time is given it just waits for hitting a key.
I put this in a .bash_env file which is sourced by any bash scripts; i put also somme other little functions and constants who are used in several scripts.

1 Like

Thanks for the HELP....