Time Counter
Create a time counter using Shell scripting.
# Create a file time.txt in home folder (any folder is ok). put '0' (zero) in it as second (starting value of time).
time.txt
# Create a time.sh script (in the same folder) and put this code in it.
This script will take stored time value from time.txt and add 60 seconds to it when run.
So if the current stored time value is '0' (zero), it will add 60 seconds to it. and so on.
# Now we will run this script with crontab. Open Terminal and edit crontab:
..and add this line to the bottom of the file:
This will make the time.sh to run and update time.txt simulteneously every minute.
# put this code in .bashrc file (found in home folder). with this we can check the time on terminal..
# Open Terminal and enter counter:
# Create a file time.txt in home folder (any folder is ok). put '0' (zero) in it as second (starting value of time).
time.txt
0
# Create a time.sh script (in the same folder) and put this code in it.
#!/bin/sh
#file: time.sh
#description: keep track of time spend on computer.
file=time.txt
if [[ -f $file ]]; then
old_time=$( cat $file ) && new_time=$[ $old_time + 60 ] && echo $new_time > $file ;
else
echo '0' > $file ;
fi;
So if the current stored time value is '0' (zero), it will add 60 seconds to it. and so on.
# Now we will run this script with crontab. Open Terminal and edit crontab:
crontab -e
..and add this line to the bottom of the file:
* * * * * /bin/bash ~/time.sh
# put this code in .bashrc file (found in home folder). with this we can check the time on terminal..
counter() {
second=$( cat ~/time.txt ) ;
day=$[ $second/86400 ] ;
hour_remain=$[ $second%86400 ] ;
hour=$[ $hour_remain/3600 ] ;
minute_remain=$[ $second%3600 ] ;
minute=$[ $minute_remain/60 ] ;
echo $day days $hour hours $minute minutes since 2014 April 12th.
}
# Open Terminal and enter counter:
0 days 0 hours 44 minutes since 2014 April 12th.
No comments:
Post a Comment