Shell编程,以函数的形式实现月份的显示,函数的作用是:在用户传送一个月份数字之后显示英文字符串形式的月份(或一条错误信息)并显示月份对应的天数。注意“平年”与“闰年”2月份天数的不同;
时间: 2023-03-28 09:04:48 浏览: 68
Shell 字符串拼接的实现示例
以下是函数的代码:
```
month() {
case $1 in
1) echo "January 31 days" ;;
2) if [ $(( $2 % 4 )) -eq -a $(( $2 % 100 )) -ne -o $(( $2 % 400 )) -eq ]; then
echo "February 29 days"
else
echo "February 28 days"
fi ;;
3) echo "March 31 days" ;;
4) echo "April 30 days" ;;
5) echo "May 31 days" ;;
6) echo "June 30 days" ;;
7) echo "July 31 days" ;;
8) echo "August 31 days" ;;
9) echo "September 30 days" ;;
10) echo "October 31 days" ;;
11) echo "November 30 days" ;;
12) echo "December 31 days" ;;
*) echo "Invalid month number" ;;
esac
}
```
使用方法:
```
month 2 2022
```
输出结果:
```
February 28 days
```
阅读全文