湖北工业大学工程技术学院实验报告
课程名称: 操作系统 实验内容: 实验二Linux Shell编程 学院: 工程技术学院 专业班级: 11gb软件2班 日期: 教师: 贺红艳 成绩: 一、实验目的
1、掌握Linux环境下vi、vim和emacs的用法。 2、掌握Linux环境下Shell程序的编写。
二、预习内容
1、shell的基本功能:
(1)命令的解释执行 (2)环境变量的设置 (3)输入/输出的重定向 (4)shell程序语言的设计
2、vi和vim或emacs的基本用法。 3、shell 的运行
在shell管理下,我们可能需要将一组命令按某种顺序执行,同时希望由系统自动完成执行过程。这时,我们可以使用编辑器编写一个shell脚本文件,UNIX系统将这种文件的后缀名规定为“.sh“。如:建立一个文件名为file1.sh的shell文件,其内容如下: touch newfile date > newfile cat newfile
执行该shell文件:sh file1.sh 4、shell 程序设计
(1)包含使用shell的版本信息 #! /bin/sh 或#! /bin/bash (2)shell变量
所有变量都为字符型,所以不用定义变量的类型,可以用 “=” 赋值,注意=的左右不能有空格。使用前面加$。输出变量用echo $val (3)位置变量
在shell中可以使用命令的位置变量进行编程,类似于C语言中的argc,argv参数的功能,用于对命令行中各个参数位置的描述。其中命令名(脚本名)对应位置变量0,第一个参数对应位置变量1,第二个参数对应位置变量2,…. (4)test命令的使用 对文件特性的测试 test –[dfrwxs] file 对字符串内容的测试 test [s,-zs,s1=s2,s1!=s2]
对整数n的测试
test n1 [-eq ne lt le gt ge] n2 (5)条件控制语句 无分支 二分支 if [condition] if [condition] then then commands commands fi else commands
fi
多分支
if [condition] then
commands
elif [commands] then commands …. … else
commands fi
case语句 case word in 1) patl –list1 2)pat2-list2 … *)default-list esac
6)循环语句 for variable
in list – of – values do commands done
三、实验步骤
while [condition] do
commands done until [condition] do
commands done
1、shell命令
编辑shell文件shellfile1.sh,内容如下:
pwd date
ps ls -l
执行上述shell过程,并记录显示结果。
2、用shell语言编制一个批处理程序shellfile2.sh,其功能如下: <1> 屏幕提示用户输入年、月;
<2> 接收用户的输入值; <3> 输出该年该月的日历。 执行该脚本,完成上述功能。 写出程序及实验结果
3、编写shell程序shellfile3.sh一次性创建10个新文件,如file1,file2,…file10
写出程序及实验结果:
四、总结
了解了一下Linux的基本一些命令,多动手实践和操作。 五、评语
相关推荐: