Tag Archives: script

Countdown timer shell script

I made a small countdown script for the shell. It goes as follows:

#!/bin/bash
# USAGE
# ./countdown <num of seconds> <msg>
# eg:
# ./countdown 300 Time to rock!
date1=$((`date +%s` + $1));
shift
while [ "$date1" -ne `date +%s` ]; do
echo -ne "$(date --date @$(($date1 - `date +%s` - 19800 )) +%H:%M:%S)\r";
done
notify-send 'Timer finished' "$*"
zenity --info --title "Timer finished" --text "$*"
view raw countdown hosted with ❤ by GitHub

Some tricks used in the script include:

  1. echo -ne "blah blah \r" was used to write in the same line over and over
  2. That 19800 is the time difference from GMT 0.0 in seconds. In Sri Lanka, it’s 5.30 hrs. Yes, we’re dealing with epoch time here, but still trying to figure out why we need to offset the time difference. 😕
  3. notify-send sends a desktop notification
  4. zenity --info pops up a message box

You can also add a sound to play with mplayer.

ShellCheck – static analyzer for shell scripts

Just came across ShellCheck. From its About page:

ShellCheck is a static analysis and linting tool for sh/bash scripts. It’s mainly focused on handling typical beginner and intermediate level syntax errors and pitfalls where the shell just gives a cryptic error message or strange behavior, but it also reports on a few more advanced issues where corner cases can cause delayed failures.

Interestingly, the tool’s completely written with Haskell. You can copy/paste the script on the site, or even install it so you can conveniently check shell scripts every now and then. You need cabal to have installed to compile the source. On Debian, you can install the package haskell-platform to install cabal.

C++ as a scripting language

Jussi Pakkanen of Canonical writes:

With the release of C++11 something quite extraordinary has happened. Its focus on usable libraries, value types and other niceties has turned C++, conceptually, into a scripting language.

I don’t necessarily agree with everything on that post, but one thing’s clear: C++ isn’t what it used to be. With C++11 things have changed, a lot.

If I was to write a script it’d be python or a shell script, I wouldn’t even consider C++. But then again, if that is some script you need to run every once in a while or one that has to be run over and over again within a short period of time, why not just write it in C++ and compile to native code? It would run faster than any other scripting language would, without waiting for some runtime or VM to load up.

And now you don’t need to learn the ins and outs of pointer arithmetic to do this. 😉

pkill script for cygwin

pkill is a basic necessity of life. For cygwin, this comes in the package procps which is not available for x64. You can of course ps and grep then awk but who has the time.

This small script from cygwin mailing list archives to the rescue:

#!/bin/sh
# pkill
# be VERY careful with parameter $1
case $1 in
'' )
cat "$0"
;;
* )
for pid in $( ps -aW | grep -i $1 | awk '{ print $4 }' );
do tskill $pid /V;
done
;;
esac
view raw pkill hosted with ❤ by GitHub

tskill is actually a kill equivalent in the Windows world.