介绍
想象你正在为当地气象局开发一个简单的天气建议系统。你的任务是完善一个 Shell 脚本,使其能够根据输入的气温提供相应的天气建议。本次挑战将测试你对 Shell 脚本中 if 语句以及基础用户输入处理的理解。
完善天气建议脚本
任务
- 切换到
~/project目录,你会发现一个名为weather_advisor.sh的未完成脚本。 - 打开
weather_advisor.sh文件,补全其中的if语句,以便根据输入的气温提供天气建议。
要求
- 脚本
weather_advisor.sh已经创建在~/project目录中,并包含基本结构。 - 脚本中已经包含了 Shebang(解释器声明)和用户输入提示。
- 你的任务是补全
if语句,以提供以下建议:- 如果气温低于 0°C:输出 "It's freezing! Wear a heavy coat and gloves."
- 如果气温在 0°C 到 10°C 之间(含):输出 "It's cold. A warm jacket is recommended."
- 如果气温在 11°C 到 20°C 之间(含):输出 "It's cool. A light jacket should suffice."
- 如果气温高于 20°C:输出 "It's warm. Enjoy the pleasant weather!"
- 使用
echo向用户显示建议。
示例
以下是完善后的脚本运行示例:
$ ./weather_advisor.sh
Enter the current temperature in Celsius: 15
It's cool. A light jacket should suffice.
$ ./weather_advisor.sh
Enter the current temperature in Celsius: -2
It's freezing! Wear a heavy coat and gloves.
$ ./weather_advisor.sh
Enter the current temperature in Celsius: 25
It's warm. Enjoy the pleasant weather!
脚本中的字符串必须参考示例且保持不变,以防止测试失败。
总结
在本次挑战中,你使用 Shell 脚本编写了一个简单的天气建议系统。你练习了如何使用 if 语句根据用户输入做出决策,并使用 echo 提供相应的输出。这次练习巩固了你对 Shell 脚本中条件逻辑的理解,并展示了这些概念在现实场景中的实际应用。



