欢迎来到CloudService文涵的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

【Linux上机实验】新实验四 shell编程

【前 言】

愿,所有相遇,都恰逢其时!

愿,此刻心头,正满怀欣喜!

                 ---你好,朋友,欢迎你!
				 
				 ---对此篇博客中有任何问题和不懂的可以咨询QQ:2759590905

1. 利用vi 建立一个脚本文件,该文件在用户输入年、月之后,自动打印数出该年该月的日历。然后以2种不同方式执行该脚本。

执行脚本方法:
方法一:以脚本名作为bash参数(格式:$bash 脚本名 [参数])
$bash mycal 回车

创建一个脚本文件,使用vi编辑器来创建一个新的脚本文件:

1.打开终端
2.输入 vi mycal.sh,创建一个名为"mycal.sh"的文件。
3.进入文本编辑模式,按下 i 键。
4.将下面的脚本内容粘贴到文本编辑器中:

#!/bin/bash
echo -n "Please enter the year:"
read year
echo -n "Please enter the month:"
read month
cal $month $year

5.退出编辑模式,按下 Esc 键,然后输入 :wq 保存并退出。

方法一:通过以下命令执行脚本

bash mycal.sh

结果截屏:
image

方法二:通过以下命令首先使脚本文件可执行,然后执行脚本

chmod u+x mycal.sh
. mycal.sh

结果截屏:
image

2.编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个单词相同显示“Two words match”,不同则显示“Two words do not match”,最后显示“End of program”。

1.打开终端
2.输入 vi word.sh,创建一个名为"word.sh"的文件。(这里的word名字可以自定义)
3.进入文本编辑模式,按下 i 键。
4.将下面的脚本内容粘贴到文本编辑器中:

#!/bin/bash
echo "Enter the first word:"
read word1
echo "Enter the second word:"
read word2
if [ "$word1" = "$word2" ]; then
	echo "Two words match"
else
	echo "Two words do not match"
fi
echo "End of program"

结果截屏:
image

3.编程使用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:
根据用户输入选项做相应操作。

1.打开终端
2.输入 vi work.sh,创建一个名为"work.sh"的文件。(这里的work名字可以自定义)
3.进入文本编辑模式,按下 i 键。
4.将下面的脚本内容粘贴到文本编辑器中:

#!/bin/bash
echo "a. Current date and time"
echo "b. User currently logged in"
echo "c. Name of the working directory"
echo "d. Contents of the working directory"
echo "Enter a, b, c, or d:"
read choice
case "$choice" in
  a)
	date
	;;
  b)
	who
	;;
  c)
	pwd
	;;
  d)
	ls
	;;
  *)
	echo "Invalid option"
	;;
esac

结果截屏:
image

4.修改上题,使用户可以连续选择直到想退出时才退出(用while语句实现)。

1.用vi打开你上一题创建的脚本文件
2.进入文本编辑模式,按下 i 键。
3.将下面的脚本内容替换之前的脚本:

#!/bin/bash
while true; do
	echo "a. Current date and time"
	echo "b. User currently logged in"
	echo "c. Name of the working directory"
	echo "d. Contents of the working directory"
	echo "e. Exit"
	echo "Enter a, b, c, d, or e:"
	read choice
	case "$choice" in
	a)
		date
		;;
	b)
		who
		;;
	c)
		pwd
		;;
	d)
		ls
		;;
	e)
		echo "Exiting..."
		break
		;;
	*)
		echo "Invalid option"
		;;
	esac
done

结果截屏:
image

5.修改上题,使用户可以连续选择直到想退出时才退出(用until语句实现)。

1.用vi打开你上一题创建的脚本文件
2.进入文本编辑模式,按下 i 键。
3.将下面的脚本内容替换之前的脚本:

#!/bin/bash
until [ "$choice" = "e" ]; do
	echo "a. Current date and time"
	echo "b. User currently logged in"
	echo "c. Name of the working directory"
	echo "d. Contents of the working directory"
	echo "e. Exit"
	echo "Enter a, b, c, d, or e:"
	read choice
	case "$choice" in
	a)
		date
		;;
	b)
		who
		;;
	c)
		pwd
		;;
	d)
		ls
		;;
	e)
		echo "Exiting..."
		;;
	*)
		echo "Invalid option"
		;;
	esac
done

结果截屏:
image

6.编程实现简单算术运算,要求用户输入一个表达式并输入结果,程序会判断用户输入的结果是否正确,并给出提示。直到用户输入‘q’时,才退出执行。

1.打开终端
2.输入 vi yunsuan.sh,创建一个名为"yunsuan.sh"的文件。(这里的yunsuan名可以自定义)
3.进入文本编辑模式,按下 i 键。
4.将下面的脚本内容粘贴到文本编辑器中:

#!/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
	if [[ number1 -eq "q" ]]
	  then 
			break;
	fi
	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 enter key"
done

1.给出执行过程及结果截屏

1.首先输出欢迎语和提示信息。
2.设置变量number1的初始值为0。
3.进入while循环,当用户输入q时循环结束。
4.提示用户输入第一个数字,并将输入的值赋给变量number1。
5.如果用户输入的是q,则跳出循环。
6.提示用户输入操作符并赋给变量oper。
7.提示用户输入第二个数字并赋给变量number2。
8.提示用户输入答案并赋给变量yourAnswer。
9.使用case语句根据操作符计算myAnswer的值。
10.检查用户输入的答案yourAnswer和程序计算的答案myAnswer是否相等,如果相等则输出"Congratulations! You are right!",否则输出"Sorry! You are wrong!"并显示正确的答案。
11.提示用户输入q退出,或者按回车键继续下一轮运算。
12.循环回第3步,直到用户输入q退出。

结果截屏:
image

2.分析该程序,理解其中的语句及用法,在程序中给出适当的注释。

这个脚本,让用户输入算术表达式并检查答案是否正确。
部分的注释:

#!/bin/bash
# 以上为shebang,指定了该脚本使用的解释器为/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;
# 初始化变量number1为0

while [ "$number1" != "q" ] 
do 
# 进入循环,直到用户输入"q"退出

echo Input the first number:
read number1
# 提示用户输入第一个数字,并将输入的值赋给变量number1

if [[ number1 -eq "q" ]]
  then 
		break;
fi
# 如果用户输入的是"q",则退出循环

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"
done
# 结束循环

【后 言】

愿,所有相遇,都恰逢其时!

愿,此刻心头,正满怀欣喜!

                 ---你好,朋友,再见,加油!
posted on 2023-11-08 08:24  Cloudservice  阅读(1353)  评论(0编辑  收藏  举报