实验三 shell编程实验(一)

一、实验目的

1.了解shell的作用和主要分类。

2.掌握bash的建立和执行方式。

3.掌握bash的基本语法。

4.综合Linux常用命令和vi编辑器的使用,熟练掌握shell脚本编程。

二、实验注意事项

实验环境与系统是共用设施,请不要在系统内做对系统或对其他用户不安全的事情。要求每个同学登录系统后,在主目录下创建一个属于自己的子目录(以自己名字(拼音缩写)或学号命名),所有工作都要在自己的目录内进行(便于备份和清理),实验结束后,请先退出ubuntu再关闭计算机,谢谢你的配合!

三、实验内容

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

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

<编程提示>请使用 if…then…else 控制结构。

  1. 编程使用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. 修改上题,使用户可以连续选择直到想退出时才退出(用while语句实现)。

 

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

  1. 编程实现简单算术运算,要求用户输入一个表达式并输入结果,程序会判断用户输入的结果是否正确,并给出提示。直到用户输入‘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

要求:

  1. 给出执行过程及结果截屏。
  2. 分析该程序,理解其中的语句及用法,在程序中给出适当的注释。

#!/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

# 结束循环

好的,我们可以开始实验三shell脚本编程。 首先,我们需要了解一些基本的shell编程语法和命令。下面是一些常用的命令: 1. echo:用于输出文本或变量的值。 2. read:用于从标准输入中读取用户输入的值。 3. if:用于条件判断。 4. for:用于循环操作。 5. while:用于循环操作。 6. case:用于多分支选择。 接下来,我们可以编写一些简单的shell脚本来练习这些命令的使用。以下是一些示例: 1. 输出Hello World ```shell #!/bin/bash echo "Hello World" ``` 2. 读取用户输入并输出 ```shell #!/bin/bash echo "Please enter your name:" read name echo "Hello, $name!" ``` 3. 判断用户输入的数字是否为偶数 ```shell #!/bin/bash echo "Please enter a number:" read num if [ $((num%2)) -eq 0 ] then echo "$num is even." else echo "$num is odd." fi ``` 4. 循环输出1-10的数字 ```shell #!/bin/bash for i in {1..10} do echo $i done ``` 5. 循环输出当前目录下所有文件名 ```shell #!/bin/bash for file in * do echo $file done ``` 6. 多分支选择 ```shell #!/bin/bash echo "Please enter a fruit name:" read fruit case $fruit in "apple") echo "It's a kind of fruit." ;; "potato") echo "It's not a fruit." ;; *) echo "I don't know what it is." ;; esac ``` 以上是一些简单的shell脚本示例,你可以根据需要进行修改和扩展。在实验过程中,你还可以尝试使用其他的命令和语法,提高自己的shell编程能力。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值