用linux输入数字表示星期几,脚本将其翻译成英文
时间: 2024-03-09 22:45:59 浏览: 25
可以使用以下脚本将数字转换为星期几的英文:
```bash
#!/bin/bash
echo -n "请输入数字表示的星期几(1-7): "
read num
case $num in
1) echo "Monday" ;;
2) echo "Tuesday" ;;
3) echo "Wednesday" ;;
4) echo "Thursday" ;;
5) echo "Friday" ;;
6) echo "Saturday" ;;
7) echo "Sunday" ;;
*) echo "输入错误,请输入1-7的数字" ;;
esac
```
你可以将这个脚本保存为 `translate_weekday.sh`,然后在 Linux 命令行下运行。输入数字后,脚本将会输出相应的英文星期几。
相关问题
linuxShell脚本代码使用if-elif结构将用户输入的数字月份转换成对应的英文表示。如果数字不在1~12内,则给出错误提示
以下是一个示例代码:
```
#!/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范围内,则给出错误提示。
linux编写一个shell脚本,使用if-else结构将用户输入的数字月份转换成对应的英文表示,如果数字不在1~12内,则给出错误提示
好的,我可以为您编写一个这样的脚本。以下是代码:
```bash
#!/bin/bash
echo "请输入月份(1~12):"
read month
if [[ $month -eq 1 ]]; then
echo "January"
elif [[ $month -eq 2 ]]; then
echo "February"
elif [[ $month -eq 3 ]]; then
echo "March"
elif [[ $month -eq 4 ]]; then
echo "April"
elif [[ $month -eq 5 ]]; then
echo "May"
elif [[ $month -eq 6 ]]; then
echo "June"
elif [[ $month -eq 7 ]]; then
echo "July"
elif [[ $month -eq 8 ]]; then
echo "August"
elif [[ $month -eq 9 ]]; then
echo "September"
elif [[ $month -eq 10 ]]; then
echo "October"
elif [[ $month -eq 11 ]]; then
echo "November"
elif [[ $month -eq 12 ]]; then
echo "December"
else
echo "错误:请输入1~12之间的数字月份!"
fi
```
使用 `read` 命令获取用户输入的数字月份,然后使用 `if-else` 结构将其转换成对应的英文表示输出。如果输入的数字不在1~12之间,则给出错误提示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)