linuxShell脚本代码使用if-elif结构将用户输入的数字月份转换成对应的英文表示。如果数字不在1~12内,则给出错误提示
时间: 2023-11-28 08:49:58 浏览: 204
以下是一个示例代码:
```
#!/bin/bash
echo "请输入一个数字月份(1-12):"
read month_num
if [ $month_num -eq 1 ]; then
echo "January"
elif [ $month_num -eq 2 ]; then
echo "February"
elif [ $month_num -eq 3 ]; then
echo "March"
elif [ $month_num -eq 4 ]; then
echo "April"
elif [ $month_num -eq 5 ]; then
echo "May"
elif [ $month_num -eq 6 ]; then
echo "June"
elif [ $month_num -eq 7 ]; then
echo "July"
elif [ $month_num -eq 8 ]; then
echo "August"
elif [ $month_num -eq 9 ]; then
echo "September"
elif [ $month_num -eq 10 ]; then
echo "October"
elif [ $month_num -eq 11 ]; then
echo "November"
elif [ $month_num -eq 12 ]; then
echo "December"
else
echo "输入的数字不在1-12范围内!"
fi
```
该脚本首先提示用户输入一个数字月份,然后使用if-elif结构将其转换成英文表示输出。如果输入的数字不在1-12范围内,则给出错误提示。
阅读全文