实验指导书--实验03 Linux Shell编程

实验三:Linux Shell编程

实验学时:2

实验类型:设计

实验要求:必修

一、实验目的

通过本实验的学习,使学生掌握Shell编程的基本方法。

二、实验内容

实验内容:Linux Shell实现题目所要求的功能。

三、实验原理、方法和手段

四、实验组织运行要求

以学生自主训练为主的开放模式组织教学

五、实验条件

PC机

六、实验步骤

1、编写一个Shell脚本,完成以下功能:

      1)显示文字“Waiting for a while….”

Vi helloworld

#!/bin/bash

Echo “Waiting for a while….” ?

2)显示当前目录下面扩展名为”.h”的文件和目录,并输出重定向到/home/file.txt文件

Find .h

Find  .h  >>/home/file.txt

提示:显示文字可使用echo命令,搜索文件可使用find命令

       2、编写一个Shell脚本,完成以下功能

              计算8以内(含8)不是3的整数倍的数字的和

       3、编写一个Shell脚本,在当前目录创建5个目录,目录的命名形式分别为:

dir-1,dir-2,….,dir-5

                    

七、思考题

比较C语言和Shell编程的一些异同点。

八、实验报告

实验预习:学习shell编程的基本方法

实验记录:记录解决问题所用的代码和运行结果

实验报告:提交代码和运行结果

九、其它说明

 

第二篇:linux-实验指导手册-shell编程

Linux实用操作系统

实验指导手册

实验二 shell编程(8学时)

一、实验名称

shell编程。

二、实验目的

掌握shell程序的建立和执行方式;掌握shell中各种变量和函数的使用;熟练掌握shell程序设计中各种控制结构语句是使用;了解shell编程中特殊字符的含义。

三、实验环境

Redhat Linux系统企业版本4.0及以上

四、实验内容

(一)shell程序的建立和执行(2学时)

1.依次执行下列命令,理解反馈信息的意义。

$ date

$ pwd

$ cd ..

$ cd

2.建立shell程序脚本

$ vi ex1

date

pwd

cd ..

cd

存盘退出。

3.检查文件是否存在。

$ ls

4.执行shell程序ex1,显示反馈信息与单步命令操作相同。

方式一:输入定向到shell脚本

$ bash < ex1

方式二:以脚本名作为参数

$ bash ex1

$ bash ex1 /usr

方式三:将shell脚本的权限设置为可执行,然后在提示符下直接执行它

步骤1:$ chmod a+x ex1

步骤2:$ mv ex1 /usr/bin

步骤3:$ ex1

        $ ex1 /usr

5. 在文本编辑器中录入下面shell程序,保存为ex2,然后执行之。

#!/bin/bash

# If no arguments, then listing the current directory.

# Otherwise, listing each subdirectory.

if  test  $# = 0

then  ls .

else

    for  i

    do

      ls  -l  $i | grep  '^d'

    done

fi

(二)shell变量(2学时)

1.用户定义的变量。

单步执行下述命令,练习变量赋值,理解反馈信息。

$ dir=/home/mengqc/ex1

$ echo $dir

$ echo dir

$ today=Sunday

$ echo $today $Today

$ str=”Happy New Year.”

$ echo  "Wish You $str"

2.read命令。

(1)单步执行下述命令:

$ read name -----输入read命令

zhangsan -----输入name的值

$ echo "Your Name is $name."

Your Name is zhangsan -----显示输出的结果

$ read a b c -----read命令有三个参数

cuit  cn  edu -----输入三个字符串,中间以空格隔开

$ echo "Email : $a. $c. $b"

Email : cuit.edu.cn -----显示输出结果

(2)将上述四个单步命令编辑为shell程序,取名ex3。

$ vi ex3      (输入四行命令,编后存盘)。

(3)运行shell程序ex3。

$ bash ex3

3.特殊变量

(1)建立一个内容如下的shell程序ex4:

echo “Program name is $0”

echo “There are totally $# parameters passed to this program”

echo “The last is $?”

echo “The parameters are $*”

(2)按如下执行程序ex4,观察反馈信息:

$ bash ex4 this is a test program

(三)控制结构(3学时)

1.if语句

(1)

理解并建立shell程序ex5:

echo “The current directory is `pwd`”

if test -f "$1"   # 如果位置参数$1对应的文件

then echo "$1 is an ordinary file."         # 是普通文件“- F”则显示本行

else echo "$1 is not an ordinary file."     # 否则显示本行

fi

执行ex5,并理解反馈信息:

$ bash ex5 ex4

(2)

理解并建立shell程序ex6:

if test -f  "$1"

  then cat $1

else  if test -d "$1"

         then (cd $1 ; cat * )

      else echo "$1 is neither a file nor a directory."

      fi

fi

执行ex6,并理解反馈信息。

(3)

验证书上例4.11、例4.12、例4.13。

2. case 语句

验证书上例4.14

2.while语句

(1)

理解并建立shell程序ex7:

while [ $1 ]

do

    if [ -f  $1 ]

    then echo "display : $1"

        cat $1

    else echo " $1 is not a file name."

    fi

    shift          #后续位置参数左移

done

执行ex7,并理解反馈信息。

(2)编写求前五个偶数之和的shell程序ex8:

loopcount=0

result=0

while [ $loopcount –lt 5 ]

do

((loopcount=loopcount+1))

((increment=loopcount * 2))

let “result=result + increment”

done    

echo "result is $result"

3.for语句

(1)

理解并建立shell程序ex9:

for day in Monday Wednesday Friday Sunday

do

    echo $day

done

执行ex9,并理解反馈信息。

(2)

理解并建立shell程序ex10:

mkdir backup

for filename in `ls`

do

   cp $filename backup/$filename

   if [ $? -ne 0 ]

then

          echo “copy for $filename failed”

   fi

done

执行ex10,并理解反馈信息。

验证书上例4.15、例4.16、4.17。

4.Select语句

理解并建立shell程序ex11:

select item in Continue  Finsh

do

     if  [ $item = “Finsh” ]; then

              break

     fi

done 

执行ex11,并理解反馈信息。

理解并建立shell程序ex12:

while true

do

select menuItem in create input delete exit

do

       case “$menuItem” in

          create) echo “create a file.”

                break;;

          input) echo “input data to file.”

                break;;

          delete)) echo “delete the file.”

                break;;

          exit) exit;;

esac

     done

done

执行ex12,并理解反馈信息。

(四)函数(1学时)

1.理解并建立shell程序ex12:

displaymonth() {

     case  $1  in

         1)  echo “Month is January”;;

         2)  echo “Month is February” ;;

         3)  echo “Month is March” ;;

         4)  echo “Month is April” ;;

         5)  echo “Month is May” ;;

         6)  echo “Month is June” ;;

         7)  echo “Month is July” ;;

         8)  echo “Month is August” ;;

         9)  echo “Month is September” ;;

         10)  echo “Month is October” ;;

         11)  echo “Month is November” ;;

         12)  echo “Month is December” ;;

          *)   echo”Invalid parameter”

    esac

}

displaymonth $1

执行ex13,并理解反馈信息。

$ bash ex12 3

$ bash ex12 10

相关推荐