一、实验目的
1.了解shell的作用和主要分类。
2.掌握bash的建立和执行方式。
3.掌握bash的基本语法。
4.综合Linux常用命令和vi编辑器的使用,熟练掌握shell脚本编程。
二、实验注意事项
实验环境与系统是共用设施,请不要在系统内做对系统或对其他用户不安全的事情。要求每个同学登录系统后,在主目录下创建一个属于自己的子目录(以自己名字(拼音缩写)或学号命名),所有工作都要在自己的目录内进行(便于备份和清理),实验结束后,请先退出ubuntu再关闭计算机,谢谢你的配合!
三、实验内容
1. 利用vi 建立一个脚本文件,该文件在用户输入年、月之后,自动打印数出该年该月的日历。然后以2种不同方式执行该脚本。
- 编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个单词相同显示“Two words match”,不同则显示“Two words do not match”,最后显示“End of program”。
<编程提示>请使用 if…then…else 控制结构。
- 编程使用case结构创建一个简单的菜单,屏幕显示菜单:
a. Current date and time
b. User currently logged in
c. Name of the working directory
d. Contents of the working directory
Enter a,b,c or d:
根据用户输入选项做相应操作。
- 修改上题,使用户可以连续选择直到想退出时才退出(用while语句实现)。
- 修改上题,使用户可以连续选择直到想退出时才退出(用until语句实现)。
- 编程实现简单算术运算,要求用户输入一个表达式并输入结果,程序会判断用户输入的结果是否正确,并给出提示。直到用户输入‘q’时,才退出执行。
参考程序:
#!/bin/bash
echo Hello! @_@
echo Welcom to the calculate testing!
echo You can input an expretion such as 2*2 or 3+1, and input the answer
echo I will tell you whether you are right or wrong.
echo You can input 'q' to exit.
echo "Now let's begin!"
number1=0;
while [ "$number1" != "q" ]
do
echo Input the first number:
read number1
echo Input the operator:
read oper
echo Input the second number:
read number2
echo Input the answer:
read yourAnswer
case $oper in
+) myAnswer=`expr $number1 + $number2`;;
-) myAnswer=`expr $number1 - $number2`;;
\*) myAnswer=`expr $number1 \* $number2`;;
/)
if [ $number2 -eq 0 ]
then
echo "Sorry! :-("
echo "0 cannot be the divisor"
continue
else
myAnswer=`expr $number1 / $number2`
fi
;;
*) echo "Error!";;
esac
if [ $myAnswer -eq $yourAnswer ]
then
echo ":-)" Congratulations!
echo Your are right!
else
echo ":-(" Sorry!
echo You are wrong!
echo "The right answer is:"
echo "$number1 $oper $number2 = $myAnswer"
fi
echo "Enter the q key to exit, continue with other keys"
read answer
if [[ $answer = q ]]
then
break
else
continue
fi
done
要求:
- 给出执行过程及结果截屏。
- 分析该程序,理解其中的语句及用法,在程序中给出适当的注释。
#!/bin/bash
# 指定解释器为bash
9
echo Hello! @_@
echo Welcom to the calculate testing!
echo You can input an expretion such as 2*2 or 3+1, and input the answer
echo I will tell you whether you are right or wrong.
echo You can input 'q' to exit.
echo "Now let's begin!"
# 提示语
number1=0;
# 变量number1初始化并赋值为0
while [ "$number1" != "q" ]
do
# 进入循环,直到用户输入"q"退出
echo Input the first number:
read number1
# 提示用户输入第一个数字,并将输入的值赋给变量number1
echo Input the operator:
read oper
# 提示用户输入运算符,并将输入的值赋给变量oper
echo Input the second number:
read number2
# 提示用户输入第二个数字,并将输入的值赋给number2
echo Input the answer:
read yourAnswer
# 提示用户输入答案,并将输入的值赋给yourAnswer
case $oper in
+) myAnswer=`expr $number1 + $number2`;;
-) myAnswer=`expr $number1 - $number2`;;
\*)myAnswer=`expr $number1 \* $number2`;;
/)
if [ $number2 -eq 0 ]
then
echo "Sorry! :-("
echo "0 cannot be the divisor"
continue
else
myAnswer=`expr $number1 / $number2`
fi
;;
*) echo Error!;;
esac
# 根据运算符计算myAnswer的值
if [ $myAnswer -eq $yourAnswer ]
then
echo ":-) Congratulations!"
echo Your are right!
else
echo ":-(" Sorry!
echo You are wrong!
echo "The right answer is:"
echo "$number1 $oper $number2 = $myAnswer"
fi
# 判断用户的答案是否正确,并给出相应的提示
echo "Enter the q key to exit, continue with enter key"
read answer
if [[ $answer = q ]]
then
break
else
continue
fi
done
# 结束循环