Reputation: 1702
I need to build simple ksh/bash scrip or whatever on Linux t that know to count time from reference time
When I type the date command as the following:
date
Wed Jul 17 18:13:27 IDT 2013
or
[root@linux /var/tmp]# current_time=` date `
[root@linux /var/tmp]# echo $current_time
Wed Jul 17 18:21:51 IDT 2013
So I get here the current date .
What I need is to count for example 10 min from the current date ,
so after 10 min I will print the message
echo “sorry 10 min was ended“
how to count time from the reference time ?
Upvotes: 2
Views: 580
Reputation: 530872
bash
has something like a built-in timer. The parameter $SECONDS
is updated continuously with the number of seconds elapsed since the shell was started. You can assign to this variable, in which case its value is essentially incremented each second.
Some examples:
$ SECONDS=0
$ sleep 10
$ echo $SECONDS
10
$ SECONDS=0
$ while (( SECONDS < 600 )); do
> sleep 10
> done
Upvotes: 4
Reputation: 5471
Use date +%s
to get Unix time - the number of seconds since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970 - and then use arithmetic expansion:
current_time=$(date +%s)
elapsed_time=$(( $(date +%s) - $current_time ))
$elapsed_time
is then the number of seconds since $current_time
. You can then check if $elapsed_time > 600
in your loop.
Edit: For completeness:
if [[ $elapsed_time > 600 ]]; then
echo "sorry 10 min was ended"
fi
This should work in both bash and ksh.
Upvotes: 1