{"id":181,"date":"2008-03-25T23:05:30","date_gmt":"2008-03-26T06:05:30","guid":{"rendered":"http:\/\/blog.oliverhansen.com\/index.php\/2008\/03\/25\/small-bash-script-to-count-seconds\/"},"modified":"2008-03-25T23:05:30","modified_gmt":"2008-03-26T06:05:30","slug":"small-bash-script-to-count-seconds","status":"publish","type":"post","link":"https:\/\/blog.oliverhansen.com\/index.php\/2008\/03\/25\/small-bash-script-to-count-seconds\/","title":{"rendered":"Small Bash Script to Count Seconds"},"content":{"rendered":"

So the short story is I got a small UPS for my tv, xbox and cable modem so that I won’t be interrupted by small power hiccups. I decided I wanted to know how long my CRT tv and xbox would continue while watching a movie so I needed something to count with while I unplugged the power to test it.<\/p>\n

Now I’m sure others have written scripts and I could probably have just used the clock on the computer but I decided it couldn’t be too difficult to figure out how to write a script to do this. I knew that I basically had to just create a loop that would print a number increasing by one and then wait one second and repeat. I just had to find the right commands. About 10 to 15 minutes of searching later and I had this:
\n<\/p>\n

#!\/bin\/bash
\n#A very basic time counting script.
\n#init value of x
\nx=1
\n#make y 1 less than x
\ny=$((x-1))
\necho \"Counting from $y...\" #Output what number we are beginning from
\n while [ $x -ge 0 ] #While $x is >= 0 (all the time)
\n do
\n sleep 1 #Wait one second
\n echo $x #Echo value of $x
\n x=$((x+1)) #Increase $x by 1
\n done
\n<\/code><\/p>\n

Some of the methods are a little different from what I am used to. To increase $x by 1, I am used to $x++. Bash is different I guess. There may be simpler ways to do this as well. If so, please leave a comment as I’m hopeful to learn.<\/p>\n

Sample output:
\n
\n$ .\/count.sh
\nCounting from 0...
\n1
\n2
\n3
\n4
\n5<\/code><\/p>\n

Also note this script will continue to run until the user types the escape sequence ‘Ctrl+C<\/em>‘.<\/p>\n

Some sources used to find what I needed:<\/p>\n