Ubuntu-Shell编程大作业

Posted 一只特立独行的猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu-Shell编程大作业相关的知识,希望对你有一定的参考价值。

文章目录

实验一:利用数据生成程序熟练掌握Shell脚本对文件夹的批处理

1.benchmark为一个可执行文件,用于生成我们需要的数据集,运行该文件的命令为./benchmark。
该程序可执行文件的输入文件为该文件所在目录下的parameters.dat,输出文件为statistics.dat,comunity.dat,network.dat,且该程序每次运行生成的数据集不同。
2.parameters.dat中包含的参数为number, averagePower, maxPower, moreno, morenO, mix,max, min,存放格式如parameters.dat所示。
3.现我们需要写一个脚本,完成如下工作。
(1)生成parameters.dat文件,用写入的方式将参数写入该文件。
(2)在相同参数下运行benchmark文件10次,并根据参数和次数新建文件夹,文件夹名字如:1000_10_30_2_1_0.3_3_16_10,最后的数字代表第多少次生成。
(3)每次产生输出文件时将三个输出文件存储在不同的文件夹,并删除该次的参数存放文件:parameters.dat文件。

#!/bin/bash
#test1.sh
#将parameter.dat.txt数据写入文件parameter.dat,并运行benchmark10次,新建文件夹,并输出,然后删除parameters.dat文件。

#创建文件parameter.dat
touch parameters.dat

#将parameter.dat.txt的内容复制到parameter.dat中
cat parameters.dat.txt>>parameters.dat

for((i=1;i<=10;i++))
do
	`./benchmark`
	`mkdir 1000_10_30_2_1_0_0.3_3_16_$i`
	`mv ./statistics.dat 1000_10_30_2_1_0_0.3_3_16_$i/statistics.dat`
	`mv ./community.dat 1000_10_30_2_1_0_0.3_3_16_$i/community.dat`
	`mv ./network.dat 1000_10_30_2_1_0_0.3_3_16_$i/network.dat`
done
rm -rf parameters.dat

运行截图

实验二:在Linux环境下编写一个压缩和解压缩的小工具

要求:压缩的时候可以由用户输入密码,解压缩的时候需要提供密码才能解压缩。

#!/bin/bash
#test2.sh
#encode to zip or unzip file
echo -e '1.zip flle\\n2.unzip file\\n'

read choice

case $choice in
	1)
		echo "please input the filename:"
		read filename
		echo "please input the zip code"
		read password
		`zip -rP $password $filename.zip $filename`
		echo "zip successfully";;
	2)
		echo "please input the filename"
		read filename
		echo "please input the zip code"
		read password
		`unzip -P $password $filename`
		echo "unzip successfully";;
esac

运行截图:

实验三:在Linux下利用case语句编写脚本,实现建立用户和删除用户的功能,过ssh连接到IP主机并保持登录的功能。

实验第一部分实现的功能为:
1.执行create时根据userfile和passfile建立用户。
2.执行delete时根据userfile删除用户。
实验第二部分实现的功能为:
1.执行:/mnt/auto_connect.exp IP password 时保证密码正确,那么就通过 ssh 连接到该 IP 主机,并保持登陆。

第一部分代码:

#!/bin/bash
#test3.sh
#create user and delete user

echo -e "please input userfilename:"
read userfile

echo -e "please input the passfile"
read passfile

echo -e "create:create new users by $userfile and $passfile\\ndelete:delete all users by $userfile"
read choice

export username

case $choice in
	'create')
		if [ ! -e $userfile ] || [ ! -e $passfile ]
		then
			echo "$userfile of $passfile do not exist"
			exit 1
		fi
		#user number
		line1=`cat $userfile|wc -l`
		
		#passwd number
		line2=`cat $passfile|wc -l`

		#judge the user number and passwd number
		echo "line1:$line1"
		echo "line2:$line2"
		if [ $line1 -ne $line2 ]
		then 
			echo "user number is not equal with passwd number"
			exit 1
		fi
		#read userfile
		for((i=1;i<=3;i++))
		do 
			read username[i]
			`sudo useradd $username[i]`
		done<$userfile

		#read passwdfile
		for((i=1;i<=3;i++))
		do
			read password[i]
			`echo $username[i]:$password[i] | sudo chpasswd`
			echo "$username[i]用户创建成功"
		done<$passfile;;
	'delete')
		if [ -e $suerfile ]
		then
			echo "deleting"
		else
			echo "file do not exist"
			exit 1
		fi
		for((i=1;i<=3;i++))
		do
			read username
			echo "$username用户删除成功"
			`sudo userdel $username`
		done<$userfile;;
esac
echo "successfully"

第二部分代码:
需要主机配置ssh环境

#!/bin/bash
#test3_plus.sh
#于主机建立ssh连接
/usr/bin/expect << EOF   
spawn ssh root@$1       
expect 
    "yes/no"  send "yes\\r";exp_continue   
    "password"  send "$2\\r"  

interact
EOF

运行截图:
第一部分:


第二部分截图:

实验四:在Linux下实现一个文件及文件夹操作的Shell脚本

1.新建一个test文件夹,在下面新建10文件夹,每一个文件夹下新建一个txt文件,并写入内容。
2.获取目录下所有文件的文件名。(包括路径)
3.将test文件夹下10个文件夹下的文件复制到test文件夹下的data文件夹中,将data文件夹中所有文件重定向到一个新的文件中。

cd `dirname $0`

mkdir test

cd $PWD/test

#打印当前路径
echo $PWD

for((i=0;i<10;i++))
do
	#新建文件夹
	eval "mkdir" "test$i"
	#新建文件
	touch test$i/new$i.txt
	#写入内容
	ls -l>test$i/new$i.txt
done

touch filenames.txt

for((i=0;i<10;i++))
do
	#进入目标文件夹
	cd test$i
	#将当前路径写入filenames.txt
	echo$PWD>>../filenames.txt
	#返回上级目录
	cd ..
done

mkdir data

for((i=0;i<10;i++))
do
	#移动文件夹
	mv test$i/new$i.txt data
	`cat data/new$i.txt>>newAll.txt`
done
#返回上级目录
cd ..





以上是关于Ubuntu-Shell编程大作业的主要内容,如果未能解决你的问题,请参考以下文章

10期末大作业

GPU 编程第六次作业(实验七)

第五次博客作业

第三次作业

R语言R语言数据可视化——东北大学大数据班R实训第三次作业

《软件测试》 第五次博客作业