概述
需求:创建一个脚本文件file.sh完成以下功能
屏幕显示日历,接收用户的选择并根据用户选择完成相应的任务。
要求:仅当用户选择0才能结束程序的运行,返回Linux,否则继续显示功能菜单接受用户的选择,将屏幕控制起来。当用户选择1时,只需输入年份,输出本年的日历,选择2时,输入年份和月份,输出当前月的日历
算法:使用循环实现屏幕控制
实现步骤
脚本如下:
#!/bin/bashch=1while [ $ch != '0' ]doecho '#######################################'echo ' 0======》Exit 'echo ' 1======》Display Calendar of month 'echo ' 2======》Display Calendar of year 'echo '#######################################'echo 'Please choose number 1,2,or 0:'read chif [ $ch = '1' ]then echo 'imput year:' read year echo 'input month:' read month cal $month $yearelse if [ $ch = '2' ] then echo 'input year:' read year cal $year else if [ $ch = '0' ] then echo 'Exit now!' else echo 'Wrong choose!' echo 'Please choose again!' fi fifiecho ' 'done
运行结果
此时如果选择1的话(需要输入年和月,并打印该月日历)
选择2的话,只需要输入年,同时打印整年的日历:
选择0的话会退出循环:
此时便完成了一个脚本,从而实现屏幕的控制,根据用户选择来输出日历了。