微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

shell脚本实战(第2版)/人民邮电出版社 脚本21 显示不同时区的时间

zonedir="/usr/share/zoneinfo/posix"

if [ $# -eq 0 ];then
	timezone="Shanghai"
	mixedzone="Asia"
elif [ "$1" = "list" ];then
	(echo "All kNown time zones and regions defined on this system:"
         cd $zonedir
         find  -L * -type f -print | xargs -n 2| awk '{printf " %-38s %-38s\n",$1,$2}')|more
	 exit 0
 else
	 region="$(dirname $1)"
	 zone="$(basename $1)"

	 matchcnt="$(find -L $zonedir -name $zone -type f -print | wc -l | sed 's/[^[:digit:]]//g')"

	 if [ "$matchcnt" -gt 0 ];then
		 if [ $matchcnt -gt 1 ];then
			 echo  "\"$zone\" matches more than one possible time zone record. Please usr list to see all kNown regions and time zones"
                         exit 1
		 fi
		 match="$(find -L $zonedir -name $zone -type f -print)"
		 mixedzone="$zone"
	 else
		 mixedzone="$(echo ${zone%${zone#?}} | tr '[[:lower:]]' '[[:upper:]]')$(echo ${zone#?} | tr '[[:upper:]]' '[[:lower:]]')"

		 if [ "mixedregion" != "." ];then
			 match="$(find -L $zonedir/$mixeregion -type f -name $mixedzone -print)"
		 else
                         match="$(find -L $zonedir -name $mixedzone -type f -print)"
		 fi
		 
		 if [ -z "$match" ];then
                         if [ ! -z $(find -L $zonedir -name $mixedzone -type d -print) ];then
				             echo "The region \"$1\" has more than one time zone."
			             else
				             echo "can't find an exact match for \"$1\"."
			             fi
			 echo "Please ust list to see all kNown regions and time zones."
			 exit 1
		 fi
	 fi
	 timezone="$match"
fi
nicetz=$(echo $timezone | sed "s|$zonedir/||g")
echo it is $(TZ=$timezone date '+%A ,%B %e,%Y,at %l:%M %p') in $nicetz
exit 0

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。