システムの日付と時刻を設定する

#!/bin/sh
#setdate.sh
#dateコマンドのフロントエンド。日付と時刻を簡単に設定できる

askvalue(){
    #$1=フィールド名,$2=デフォルト値
    #$3=最大値,$4=必要な文字数/桁数
    echo -n "$1 [$2] : "
    read answer
    if [ ${answer:=$2} -gt $3 ]; then
        echo "$0: $1 $answer is invalid"
        exit 0
    elif [ "$(( $(echo $answer | wc -c) - 1 ))" -lt $4 ]; then
        echo "$0: $1 $answer is too short: please specify $4 digits"
        exit 0
    fi
    eval $1=$answer
}

eval $(date "+nyear=%Y nmon=%m nday=%d nhr=%H nmin=%M")

askvalue year $nyear 3000 4
askvalue month $nmon 12 2
askvalue day $nday 31 2
askvalue hour $nhr 24 2
askvalue minute $nmin 59 2

squished="$year$month$day$hour$minute"

#echo "Setting date to $squished. You migth need to enter your sudo password: "
#sudo date $squished
#echo $squished
date  $squished

exit 0