Himanshu
Himanshu

Reputation: 49

How to set the timer in shell

Can we set the timer in shell script? means if I set the timer for 20 secs my shell scripts should be execute like this :

20..19..18..17..

after completing the I should get the result. Is it possible ? Please assist me.

Upvotes: 1

Views: 5579

Answers (2)

user14642966
user14642966

Reputation:

With for loop it looks like:

#!/bin/bash
for (( i=20; i>0; i-- ))
do
clear
echo -n "$i"
sleep 1
done

Upvotes: 0

William Pursell
William Pursell

Reputation: 212248

It's not entirely clear what you want: if xyz.sh finishes in 3 seconds, do you want to wait an additional 17 seconds? Or do you want to interrupt xyz.sh after 20 seconds and make it produce output? If the latter:

$ cat a.sh
#!/bin/bash 

./xyz.sh &

i=${1-20}
echo
while test $i -gt 0; do printf "\r$((i--))..."; sleep 1; done
kill $!


$ cat xyz.sh 
#!/bin/sh

foo() { echo "this program doesn't do much"; }
trap foo TERM
sleep 30 &
wait

Upvotes: 0

Related Questions