操作系统leb1实验报告

实验名称:实验1:bash脚本编程

  1. 实验目的
  • 1、熟悉Linux系统下bash编程
  • 2、利用bash编程完成简易功能

实验内容

  1. 编写一个脚本文件checkuser,该脚本运行时带一个用户名作为参数,具体要求如下:
    • 如果命令行格式不符合要求,应该有错误提示信息。
    • 在/etc/passwd 文件中查找是否有该用户,如果有则输出”Foundin the /etc/passwd file”;否则输出“No such user on our system.”。
  2. 写一个脚本文件printernumber,该脚本运行时带一个数值参数,参数可以包含小数部分。具体要求如下:
    1. 如果命令行格式不符合要求,应该有错误提示信息。
    2. 小数点前从个位开始。每三位为一节,节与节之间应该有逗号分开。

实验环境

  1. VMware
  2. Ubuntu

实验作业

一、注释与简单命令

一个简单的bash文件想要执行,首先需要被赋予可执行权限,否则无法在系统中运行。 Linux/Unix 的文件调用权限分为三级 : 文件所有者(Owner)、用户组(Group)、其它用户(Other Users),只有文件所有者和超级用户可以修改文件或目录的权限。可以使用绝对模式(八进制数字模式),符号模式指定文件的权限。
语法:chmod [-cfvR] [--help] [--version] mode file...
参数: mode: 权限设定字串,格式如下 : [ugoa...][[+-=][rwxX]...][,...]

     - `u `表示该文件的拥有者,`g` 表示与该文件的拥有者属于同一个群体(group)者,`o`表示其他以外的人,`a` 表示这三者皆是。
     - `+` 表示增加权限、`- `表示取消权限、`= `表示唯一设定权限。
     - ​`r `表示可读取,`w` 表示可写入,`x` 表示可执行,X 表示只有当该文件是个子目录或者该文件已经被设定过为可执行。

其他参数说明:

-c : 若该文件权限确实已经更改,才显示其更改动作
-f : 若该文件权限无法被更改也不要显示错误讯息
-v : 显示权限变更的详细资料
-R : 对目前目录下的所有文件与子目录进行相同的权限变更(即以递归的方式逐个变更)
--help : 显示辅助说明
--version : 显示版本

示例代码:

#!/bin/bash
#name:hello
echo 'hello,world'//输出字符串

如果想要运行该脚本程序,需要为他赋予可执行权限:
语句:

$chmod +x hello//控制用户对文件的权限的命令
$./hello	//运行生成文件

其中的注释由#开始,直至行位结束。但是#!/bin/bash较为特殊,他表示这是一个bash脚本,需要用命令解释器来执行。
运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/a320fbb539ec3def794936058e769b01.png#clientId=u1cd65eb3-b930-4&from=paste&height=131&id=ue3177c53&margin=[object Object]&name=image.png&originHeight=174&originWidth=353&originalType=binary&ratio=1&size=31093&status=done&style=none&taskId=u9874b912-a442-4f28-88d1-ab212b4b05e&width=265)
![image.png](https://img-blog.csdnimg.cn/img_convert/0c0f98be0c1f5669da3e12129fb53bbf.png#clientId=u1cd65eb3-b930-4&from=paste&height=56&id=u6733bed4&margin=[object Object]&name=image.png&originHeight=75&originWidth=391&originalType=binary&ratio=1&size=50549&status=done&style=none&taskId=ub2812e7d-499f-4877-85cc-7a7508a7161&width=293)

二、环境变量

在bash中定义变量不需要什么数据类型,在使用之前要加上$,使用各种字符的时候要注意空格的使用,否则会对关键字的应用产生影响。
环境变量可以向子进程传递数据:export $msg
此外还有许多的功能,可以联机查看。
测试代码:

msg='hello ,world!'
echo $msg

运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/fcb5b23dda5a8cf83e849a6e46853d93.png#clientId=u1cd65eb3-b930-4&from=paste&height=152&id=u7faf616a&margin=[object Object]&name=image.png&originHeight=203&originWidth=404&originalType=binary&ratio=1&size=40524&status=done&style=none&taskId=u3d7fef4e-877a-4223-9a44-19a598ee784&width=303)
![image.png](https://img-blog.csdnimg.cn/img_convert/a2b3d89bfef1dfb4558d9d2c1b893f2d.png#clientId=u1cd65eb3-b930-4&from=paste&height=60&id=u1cec766e&margin=[object Object]&name=image.png&originHeight=80&originWidth=490&originalType=binary&ratio=1&size=59779&status=done&style=none&taskId=u3d1f1e5c-f387-4791-8316-c091efb105d&width=368)

三、控制结构

if语句

语法:

 if test-commands
    then
       Consequent-commands;
    else
       Other
 fi
含义:如果`test-commands`的返回值为0,则执行下一步执行命令,否则进入else。Elif可以有多个,也可以没有。

测试代码:

#!/bin/bash
if [ "${1##*.tar.}"="gz" ];
then
	echo It appears to be a tarball zipped by gzip.
else
	echo It appears to NOT be a tarball zipped by gzip.
fi

测试图:

![image.png](https://img-blog.csdnimg.cn/img_convert/c6138d7c2c6828d9d0d6d96bb982085c.png#clientId=u1cd65eb3-b930-4&from=paste&height=173&id=u09145d75&margin=[object Object]&name=image.png&originHeight=230&originWidth=588&originalType=binary&ratio=1&size=90281&status=done&style=none&taskId=ue69c3f8f-2862-4067-9351-08b8a6a3b36&width=441)
![image.png](https://img-blog.csdnimg.cn/img_convert/5dc926622e0933e2cffe335e8ea7e848.png#clientId=u1cd65eb3-b930-4&from=paste&height=33&id=u0efc78ae&margin=[object Object]&name=image.png&originHeight=44&originWidth=409&originalType=binary&ratio=1&size=42046&status=done&style=none&taskId=uf012081c-e656-45de-b24e-b2d2b041889&width=307)

Case语句

语法:

case word in
[[)]pattern[| pattern]...)
Command-list ;;]
 .....
esac

含义:寻找与word相匹配的第一个pattern,然后执行对应的命令列表command-list,如果没有匹配任何字符串或模式,则不执行任何代码。
测试代码:

#!/bin/bash
if [ -e "$1" ]
then
	case ${1##*.} in 
	bz2)
		tar jxvf $1
		;;
	gz)
		tar zxvf $1
		;;
	zip)
		unzip $1
		;;
	*)
		echo "wrong file type"
		;;
	esac
else
	echo "the file $1 does not exist"
fi

运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/f7840500bf2dbd72244306fd70b3cd54.png#clientId=u7e3e4133-4a32-4&from=paste&height=354&id=u6e464a92&margin=[object Object]&name=image.png&originHeight=471&originWidth=418&originalType=binary&ratio=1&size=111459&status=done&style=none&taskId=u7102c735-1fe2-4f65-ab70-41aa4e81521&width=314)
![image.png](https://img-blog.csdnimg.cn/img_convert/9f49a608d9e8f5ac6011249b031adb84.png#clientId=u7e3e4133-4a32-4&from=paste&height=82&id=u5d191fe0&margin=[object Object]&name=image.png&originHeight=109&originWidth=561&originalType=binary&ratio=1&size=94026&status=done&style=none&taskId=udea7af99-06c5-412d-ac4e-e73634686fd&width=421)

For语句

语法:

for name [in words ...];
    do
       Commands;
    done

含义:对于word中的每一个字符串,都将其赋给变量name,然后执行命令列表sommands
测试代码:

#!/bin/bash

echo $0
echo $#
echo $*
for para in "$@"
do
	echo ${para}
done

运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/86b5115989708cc14fde1b6188b55ed8.png#clientId=u7e3e4133-4a32-4&from=paste&height=173&id=u88166172&margin=[object Object]&name=image.png&originHeight=230&originWidth=344&originalType=binary&ratio=1&size=53087&status=done&style=none&taskId=u1c1e0386-bf25-49b5-8a1c-2f4e1b1816a&width=258)
![image.png](https://img-blog.csdnimg.cn/img_convert/9e18510001626a17ccca2fe75c82b69f.png#clientId=u7e3e4133-4a32-4&from=paste&height=150&id=ub4f30223&margin=[object Object]&name=image.png&originHeight=200&originWidth=529&originalType=binary&ratio=1&size=71022&status=done&style=none&taskId=u675204b9-8afb-4a06-92fb-234bfa99c9c&width=397)

While和until语句

While的语法:

while test-commands;
do
Consequent-commands
done

含义:只要命令列表test-commands的返回结果为0,就执行命令列表,不断重复此过程,直至返回结果为非0;
测试代码:提示用户输入一个字符串,如果输入的是end就结束,否则继续读入

#!/bin/bash
unset var
while [ "$var"!="end" ]
do
	echo -n "please input a strin(\"end\" to exit):"
	read var
	if [ "$var" = "end" ]
	then 
	break
	fi
	echo "the string you input id $var"
done

运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/b046f2d00ea911e28e62ef88d939fd87.png#clientId=u7e3e4133-4a32-4&from=paste&height=326&id=uc7de2742&margin=[object Object]&name=image.png&originHeight=435&originWidth=520&originalType=binary&ratio=1&size=99825&status=done&style=none&taskId=u1f3ec8f1-d44d-44a6-84e5-ac5657d0f64&width=390)
![image.png](https://img-blog.csdnimg.cn/img_convert/535993ffeb5d529786f8dc6f3aac0872.png#clientId=u7e3e4133-4a32-4&from=paste&height=238&id=u9d0beaf5&margin=[object Object]&name=image.png&originHeight=317&originWidth=598&originalType=binary&ratio=1&size=42471&status=done&style=none&taskId=uf61b97d7-2596-4548-a185-74e0590ed72&width=449)

四、函数

在bash脚本中,也可以定义与过程式语言类似的函数,并且bash脚本中的函数也可以接受和处理命令行参数其方式类似于脚本对命令行参数的使用。
运行代码:

#!/bin/bash
myunnzip()
{
	if [ -e "$1" ]
	then
		case ${1##*.tar.} in
		bz2)
		tar jxvf $1
		;;
		gz)
			tar zxvf $1
			;;
		*)
			echo "wrong file type"
			;;
		esac
	else
		echo "the file is not find"
	fi
		
}
myunnzip a.tar.gz
myunnzip input.tar

运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/18591456fa330fb0164cd0bb01750e05.png#clientId=u7e3e4133-4a32-4&from=paste&height=389&id=u4e1fd72b&margin=[object Object]&name=image.png&originHeight=519&originWidth=519&originalType=binary&ratio=1&size=166157&status=done&style=none&taskId=u48c6a2af-7d73-44ea-86ff-d28554f5bb6&width=389)
![image.png](https://img-blog.csdnimg.cn/img_convert/59fc1d3faa9e603c04391c2dba2ea287.png#clientId=u7e3e4133-4a32-4&from=paste&height=51&id=u2657d3b2&margin=[object Object]&name=image.png&originHeight=68&originWidth=486&originalType=binary&ratio=1&size=58158&status=done&style=none&taskId=uf00e1504-8148-42b7-8035-2bd9649220d&width=365)

五、实验结果

实验一:checkuser

编写一个脚本文件checkuser,该脚本运行时带一个用户名作为参数,具体要求如下:

  • 如果命令行格式不符合要求,应该有错误提示信息。
  • 在/etc/passwd 文件中查找是否有该用户,如果有则输出”Foundin the /etc/passwd file”;否则输出“No such user on our system.”。

需求知识点:
![image.png](https://img-blog.csdnimg.cn/img_convert/ea5b4417bd36a1de20a028b9774c87d6.png#clientId=u7e3e4133-4a32-4&from=paste&height=311&id=u6478e75f&margin=[object Object]&name=image.png&originHeight=414&originWidth=502&originalType=binary&ratio=1&size=74291&status=done&style=none&taskId=u6f176ede-c281-47b6-9b5a-5c94a151a69&width=377)
实验代码:

#!/bin/bash

echo input your user name

read username

if [ 'cut /etc/passwd -d: -f1 |grep -c $username'  ]
then 
	echo i can find
else
	echo i can not find
fi



运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/868b87d1bbcd298c6d83a5a08d6d8427.png#clientId=u7e3e4133-4a32-4&from=paste&height=290&id=ub439b189&margin=[object Object]&name=image.png&originHeight=387&originWidth=652&originalType=binary&ratio=1&size=44074&status=done&style=none&taskId=ubdbdd942-8192-4acf-9cf6-ae6a5b7d196&width=489)
![image.png](https://img-blog.csdnimg.cn/img_convert/65a0def9f5a5517363ef67a6b24ae53d.png#clientId=u7e3e4133-4a32-4&from=paste&height=72&id=uaeb2dea1&margin=[object Object]&name=image.png&originHeight=96&originWidth=417&originalType=binary&ratio=1&size=16216&status=done&style=none&taskId=ube7dd324-7545-4724-ae03-65d9c02e483&width=313)

实验二:printnumber

写一个脚本文件printernumber,该脚本运行时带一个数值参数,参数可以包含小数部分。具体要求如下:

  1. 如果命令行格式不符合要求,应该有错误提示信息。
  2. 小数点前从个位开始。每三位为一节,节与节之间应该有逗号分开。

运行代码:

#!/bin/bash

if [ $# != 1 ]
then
echo "error"
exit 1

else
printf " '.10g\n" &1
fi

运行图:
![image.png](https://img-blog.csdnimg.cn/img_convert/de8fd9e45da3b2333301781aeef64924.png#clientId=u7e3e4133-4a32-4&from=paste&height=206&id=u48952e51&margin=[object Object]&name=image.png&originHeight=275&originWidth=308&originalType=binary&ratio=1&size=15922&status=done&style=none&taskId=u370f1bd8-534a-403e-abae-72ede19168d&width=231)
![image.png](https://img-blog.csdnimg.cn/img_convert/90cc2e3bd0d543af274d0c6b93216881.png#clientId=u7e3e4133-4a32-4&from=paste&height=39&id=u22927b79&margin=[object Object]&name=image.png&originHeight=52&originWidth=670&originalType=binary&ratio=1&size=10687&status=done&style=none&taskId=u162eb6e0-ac49-4390-87cb-b759fe8c111&width=503)

六、实验体会

在本次实验中,我对从课本所学知识进行应用,学到了基础的bash编程,完成了简单的利用。加深了我对bash脚本语言的理解和认识,在实验过程中,更容易记忆和深入理解各种脚本命令以及命令行语言工作还有在bash编程中应该注意的一系列问题。
在本次实验中,揭开了脚本语言神秘的面纱。当然在实验中出现了很多的插曲,查阅书籍和网上咨询才弄懂了他们的来龙去脉,最终完成了练习。开始接触bash语言一头雾水。在看了网上的介绍和讲解后才有所起色,了解到最主要还是命令行的使用以逻辑框架的搭建,才能够自己编写程序。我最深的体会就是对具体的内部细节不熟悉,导致在调试的过程中感觉到很无力。各种命令行的含义不熟悉。在以后要经常将用到的命令的用法温习一下,最后还是能够解决很多的问题的。
本次试验是操作系统实验的lab1。这次的实践对我们的挑战还算不是太大,通过努力,所有的困难被克服掉了。在以后的工作中要的就是我们的实际的动手能力,如果我们在学习期间就是只学了书本上的知识,那样对理论的了解是不够深刻的,只有通过实验才能激发我们的学习兴趣。实验才是检验理论的唯一标准。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值