linux实验报告--shell编程

广东科学技术职业学院

计算机工程技术学院(软件学院)

实 验 报 告

专业  计算机网络技术             班级          成绩评定______

学号    姓名              (合作者____号____)  教师签名  赖小卿 

实验     题目 shell编程

   周星期     

 

第二篇:Linux实验报告 shell脚本编程

南京信息工程大学实验(实习)报告 实验(实习)名称 shell脚本编程 实验(实习)日期 得分 指导教师 系 计算机 专业 计算机科学与技术 年级 班次 姓名 学号 一·【实验目的】

1、了解和熟悉创建并使用脚本的步骤。

2、熟悉bash 的控制结构。

3、学会简单的shell 编程。

二·【实验内容】

1、创建一个简单的列目录和日期的shell脚本并运行之。

步骤:

⑴输入下列命令,创建一个新文件:

cat >new_script

⑵输入下列行:

echo “Your files are”

ls

echo “today is”

date

按回车键将光标移到一个新行,按Ctrl+D键保存并退出。

⑶检查文件内容,确保它是正确的:

Linux实验报告shell脚本编程

#cat new_script

⑷运行脚本,输入它的文件名:

Linux实验报告shell脚本编程

#new_script

该脚本不运行。

⑸输入下列命令,显示文件的权限:

#ls –

Linux实验报告shell脚本编程

l new _script

权限表明该文件不是可执行。要通过简单调用文件名来运行脚本,必须有权限。 ⑹输入下列命令,使new_script 变成可执行文件。

chmod +x new_script

⑺要查看新的权限,输入:

ls –l

Linux实验报告shell脚本编程

现在拥有文件的读、写和执行权限。

⑻输入新脚本的名字以执行它:

new_script

所有输入到文件的命令都执行,并输出到屏幕上。

⑼如果接收到错误信息,比如:

command not found

输入下列命令:

Linux实验报告shell脚本编程

#./new_script

日 10月 11 12:37:00 CST 2009

该命令行通知shell 到哪里寻找shell 脚本new_script,即您的当前目录“.”。

2、用Shell 语言编制一Shell 程序,该程序在用户输入年、月之后,自动打印数出该年该 月的日历。

<参考程序>

echo “Please input the month:”

read month

echo “Please input the year:”

read year

Linux实验报告shell脚本编程

cal $month $year

Linux实验报告shell脚本编程

3、编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个单词相同则显 示“Match”,并显示“End of program”,如果不同则显示“End of program”。 <参考程序> 提示:echo –n 是显示内容,但不回车换行

$ cat > if1

echo –n “word 1:”

read word1

echo –n “word 2:”

read word2

if test “$word1” = “$word2”

then

echo “Match”

fi

echo “End of program.”

<程序说明>

①if?then 控制结构的语法是:

if test_command

then

commands

fi

②其中test_command 为test “$word1” = “$word2”, test 是一个内置命令,如果它的 第一个参数和第三个参数存在第二个参数所指定的关系,那么test 将返回ture。Shell 将执 行then 和fi 之间的命令。否则执行fi 后面语句。

4、修改上述程序,编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果 两个单词相同显示“Match”,不同则显示“Not match”,最后显示“End of program”。 <编程提示>请使用if?then?else 控制结构。

Linux实验报告shell脚本编程

5、编程使用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:

根据用户输入选项做相应操作。

<参考程序>

echo –e “\n COMMAND MENU\n”

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\n”

echo –n “Enter a,b,c or d:”

read answer

echo

case “$answer” in

a)date;;

b)who;;

c)pwd;;

d)ls;;

*)

Echo “There is no selection : $answer”;;

esac

6、修改上题,使用户可以连续选择直到想退出时才退出。

While[t]

Do

clear

echo –e “\n COMMAND MENU\n”

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\n”

echo –n “Enter a,b,c or d:”

read answer

echo

case “$answer” in

a)date;;

b)who;;

c)pwd;;

d)ls;;

e)return;;

*)Echo “There is no selection : $answer”;;

esac

done

7、编程使用select结构生成一个菜单如下:

1)apple 3)blueberry 5)orange 7)STOP

2) banana 4)kiwi 6)watermelon

Choose your favorite fruit from these possibilities:

用户输入所选项,如 1 显示:

You chose apple as you favorite.

That is choice number 1.

<参考程序>

#!/bin/bash

PS3=“Chose your favorite fruit from these possibilities:”

select FRUIT in apple banana blueberry kiwi orange watermelon STOP do

if [ $FRUIT = STOP ] then

echo “Thanks for playing!”

break

fi

echo “You chose $FRUIT as you favorite.”

echo “That is choice number $REPLY.”

echo

done

<程序说明>

do select 结构的语法如下: select varname[in arg?]

commands

done

②REPLY 是键盘变量。

8、上机编写如下代码,分析下面的程序,简要说明整体功能,并解释每条语句。

#!/bin/sh

val=1

while (test $val -lt 6)

do touch file$val

date>>file$val

val=`expr $val + 1`

done

9、编写一段shell程序,根据执行时候获取的当前时间显示出不同的问候信息。

#!/bin/bash

a=`date +%H`

if (test $a -lt 12);

then

echo "Good Morning!"

else

echo "Good Afternoon!"

fi

10、用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到/dev 目录下

myfile=/path/filename # 换成你实际的文件全路径

fd=`ls -l myfile` # 获取文件详细信息

fp=${fd:0:1} # 截取第一个属性值

[ "$fp" = "c" ] && cp myfile /dev # 如果该属性值为c,则为字符设备文件,拷贝到/dev目录

11、在根目录下有四个文件m1.txt,m2.txt,m3.txt,m4.txt,用Shell 编程,实现自动创

建m1,m2,m3,m4四个目录,并将m1.txt,m2.txt,m3.txt,m4.txt四个文件分别拷贝到各自相应的目录下 #!/bin/bash

for var in 1 2 3 4

do

mkdir "m$var"

mv /"m$var.txt" /"m$var"/

done

相关推荐