このブログを検索

2013/05/01

備忘録

unset_linguas () {
export LC_ALL=C
export LC_MESSAGES=C
export LANGUAGE=C
export LANG=C
}

make_tmp_dir () {
local TMP_ROOT="${1:-${TMP_ROOT:-${HOME}}}"
local tmpdir=$(basename "$0")$$

tmpdir="${TMP_ROOT}/${tmpdir}"

mkdir "${tmpdir}" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "${tmpdir}"
chmod 700 "${tmpdir}"
else
echo "Can't create cirectory: ${tmpdir}" 1>&2
return 1
fi
}


clean_tmp_dir () {
local TMP_ROOT="${1:-${TMP_ROOT:-${HOME}}}"
local tmpdir=$(basename "$0")$$

tmpdir="${TMP_ROOT}/${tmpdir}"

if [ ! -d "${tmpdir}" ]; then
echo "No such directory: ${tmpdir}" 1>&2
return 1
fi

if [ -L "${tmpdir}" ]; then
echo "Can't delete unexpected symlink: ${tmpdir}" 1>&2
return 1
fi

rm -rf "${tmpdir}" > /dev/null 2>&1

}


#!/bin/bash

MAIL_FROM="hoge@example.com"
MAIL_TO="foo@example.com"
MAIL_SUBJECT="テストメール"
MAIL_ID="<$(date +%Y%m%d%H%M%S).$(id -ru).$$@$(hostname)>"
SENDMAIL=/usr/lib/sendmail

FILE_UTIL=util_common.bash
. "${FILE_UTIL}" 2> /dev/null \
|| . "${0%/*}/${FILE_UTIL}" 2> /dev/null
if [ $? -ne 0 ]; then
echo "Can't load ${FILE_UTIL}!" 1>&2
exit 1
fi

unset_linguas

tmpdir=$(make_tmp_dir 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Can't create temporary directory!" 1>&2
exit 2
fi

SIGNALS="HUP INT QUIT PIPE TERM"
trap "{ clean_tmp_dir; exit; }" ${SIGNALS}

subject=$(echo -n "${MAIL_SUBJECT}" |
iconv -f UTF-8 -t ISO-2022-JP |
perl -MMIME::Base64 -ne 'print encode_base64($_)')

mail_data="${tmpdir}/mail_data"
cat <<EOF > "${mail_data}"
Mime-Version: 1.0
Content-Type: Text/Plain; charset=iso-2022-jp
Content-Transfer-Encording: 7bit
From: ${MAIL_FROM}
To: ${MAIL_TO}
Subject: =?ISO-2022-JP?B?${subject}?=
Message-Id: ${MAIL_ID}
Date: $(date +"%a, %e %b %Y %H:%M:%S %z")

テストメールです。
-----------------------------------------------------
UPTIME
-----------------------------------------------------
$(uptime)

-----------------------------------------------------
W
-----------------------------------------------------
$(w)

-----------------------------------------------------
FREE
-----------------------------------------------------
$(free)

EOF

iconv -f UTF-8 -t ISO-2022-JP "${mail_data}" |
"${SENDMAIL}" -oi "${MAIL_TO}" > /dev/null 2>&1

clean_tmp_dir
trap ${SIGNALS}


#!/bin/bash

select member in john paul george ringo
do
echo "You chose $member."
break
done


while :; do netstat -tp; sleep 5; done