The Archives

  • 07.Apr.10
    Setting a Timer scripts | rafacas | (3)
    Sometimes I need a timer to focus on something and to alert me when to stop. Remember, we are real commandliners, so we do not want those fancy applications with a lot of features, we need a script ;-) so here it is: #!/bin/bash usage() { name=`basename $0` echo "Usage: $name hh:mm:ss" echo "Example: $name \"00:15:30\"" } if [ $# != 1 ] then usage exit fi IFS=: set -- $* secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} )) while [ $secs -gt 0 ] do sleep 1 & printf "\r%02d:%02d:%02d" ...
  • 02.May.09
    Saving bash lines shell | rafacas | (0)
    Thanks to the short-circuit behaviour of the && operator, if-statements can be replaced by: [[ test ]] && if_true_do_this || otherwise_do_that So, instead of writing: if [[ "$1" == "$2" ]]; then echo "$1 equals $2" else echo "$1 differs from $2" fi One can write: [[ "$1" == "$2" ]] && echo "$1 equals $2" || echo "$1 differs from $2" Braces can be used to run more than a single command: [[ test_condition ]] && { true_stuff_1; true_stuff_2; } || { false_stuff_1; false_stuff_2; } Via | commandlinefu
  • 14.Feb.09
    Is your site up? scripts | rafacas | (0)
    When I cannot reach one of my sites, I do not know if it is my problem, my ISP's or my hosting provider's. This is why I use the service down for everyone or just me. I have written a simple script which uses that service. Its usage is: $ ./is_up.sh commandliners.com commandliners.com is up and the script is: #!/bin/bash # is_up.sh # 2009 - Rafa Casado - rafacas@commandliners.com numargs=$# if [ $numargs -ne 1 ] then echo "Usage: $0 domain" echo "Ex: $0 commandliners.com" exit 1 fi DOMAIN=$1 IS_UP=`wget -nv -O ...
  • 02.Feb.09
    Currency conversion script scripts | rafacas | (1)
    In this time of crisis perhaps we need to pay more attention to financial issues. I am interested in converting some currencies so I have written a script that given two currencies, converts on to the other. The script searches the info on Google Finance's page. An example of its usage is: $ ./currencies.sh EUR USD 1 EUR = 1.316 USD The available currencies are: EUR - Euros USD - United State Dollar GBP - British Pound JPY - Japanese Yen CHF - Swiss Franc CAD - Canadian Dollar AUD - Australian Dollar INR - Indian ...
  • 02.Dec.08
    Quiet grep automated, shell | pfortuny | (0)
    Usually during an cron job, one needs to check for an expression in a log file, or in the output of a command, without generating any cumbersone new output (such as a standard grep would do): this is exactly the use of the -q option of grep. For example: for i in `grep -v '^#' /etc/passwd | cut -f1 -d:` grep $i /var/log/samba/log.smbd | grep -q 'failed to authenticate' if [ $? == 0 ] ; then mail -s 'Error in samba' $i <<EOM ...