B-shell tips
Change separators for a sequence of for-in statement
A reserved variable IFS (Internal Field Separator) defines a set of characters of separators for a sequence of for-in statement. For example, configure the variable as follows if you want to split a sequence by ",", by neither any whitespaces nor line breaks.
$ export IFS=','
A usage example is shown below.
$ IFS=','; for x in `echo 'a,b,c'`; do echo $x; done a b c
mailout.sh — Notifying command completion with the exit status via E-mail
The following code is a shell script that notifies you the command completion with the exit status via E-mail. You can also download the source code from the link mailout.sh.
#!/bin/sh
#
# $Id: mailout.sh,v 966c5717bd73 2011/01/20 13:53:16 Hirochika $
#
# Copyright 2011 Scyphus Solutions Co. Ltd. All rights reserved.
# Authors:
# Hirochika Asai
#
if [ -z "$1" -o -z "$2" ];
then
echo "Usage: $0 " >& 2
exit 1
fi
mailaddr=$1
cmd=$2
args=""
num=3
while [ $num -le $# ];
do
a0=`eval echo \$\{$num\}`
a2=`echo ${a0} | sed -e "s/\ /\\\\\ /g"`
args="${args} ${a2}"
num=`expr $num + 1`
done
eval ${cmd} ${args}
ret=$?
## Mail the result
dt=`LC_ALL=C date`
printf "The following command exits with status ${ret}:\n\$ ${cmd} ${args}\n@${dt}\n" | mail -s "Command completion notification (${dt})" "${mailaddr}"
The following command is a usage example:
$ ./mailout.sh notify@example.jp sleep 3600
In this example, after 3600 seconds (i.e., 1 hour), this command notifies the exit status code to the email address <notify@example.jp>.