bupt本科-Linux基础(董远)-某年期末大作业

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

LINUX系统与程序设计2011春本科生大作业

------------------------------------------

1. Shell编程:查询系统最近1000条命令中使用最多的前10条命令,编写Shell脚本实现此功能。写出具体代码和操作过程。

------------------------------------------

# echo $HISTSIZE 1000

# history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10

解释:第一行的echo $HISTSIZE 1000是设置history中记录1000条命令;第二行中用awk 统计history 中的命令部分出现的次数,去掉“./" 这样不算命令的情况,最后排序输出,列出前10 个负责统计最近1000条命令中使用最多的10条命令。

-------------------------------------------

2. Linux操作系统的系统配置文件默认储存在/etc目录下,因此管理员可以通过查看相应的文件来得知系统配置信息。编写一个shell脚本显示下列系统配置信息:

(1)CPU相关信息:如处理器类型,速度等;

(2)内存信息;

(3)挂载文件信息。

-------------------------------------------

说明:通过在网上查到的资料,以及ls对/etc目录的列表,没有发现系统配置信息存在于/etc 中,而应该是/proc中。

#ls -a /etc

#ls /etc/cpuinfo

#ls /etc/meminfo

#ls /etc/mount

-------------------------------------------

3. 编写my_cp.sh脚本,功能是把一个文件拷贝到另一文件。其有两个参数,一个作为输入文件(source),一个作为输出文件(destination)。脚本需满足下面一些条件:(1)确保输入的两个参数不重名;

(2)验证输出文件(destination)是一个文件,而不是目录;

(3)验证输入文件(source)文件存在;

(4)检查输出文件是否存在,若存在,则询问用户是否覆盖。

-------------------------------------------

#! bin/sh

sign=1

if

[ "$#" != 2 ]

then

echo "please make sure that you enter 2 arguments, first is the source file, second is the destination directory."

exit 1

fi #if 1 , check the amount of the arguments.

if

[ ! -f "$1" ]

then

echo "the source file(the first argument) doesn't exist, please check your arguments" exit 1

fi #if 2 , check if the source file exists.

if

[ "$1" = "$2" ]

then

echo "the source file and the destination is the same file!"

exit 1

fi #if 3 , check the amount of the arguments.

if

echo "$2" | grep -q '^[a-zA-Z0-9_]\+$'

then

continue

else

echo "your destination is a invalid file name."

exit 1

fi #if 4, check if the destination is a invalid file name

if

[ -f "$2" ]

then

while [ $sign -eq 1 ]

do

echo "the destination file has been created, are u sure to overwrite it?(please enter y/n)" read YesOrNo

case "$YesOrNo" in

y)

cp $1 $2

echo "------------------------------------------"

echo "the file has been copied"

sign=0

continue

;;

Y)

cp $1 $2

echo "------------------------------------------"

echo "the file has been copied"

sign=0

continue

;;

n)

sign=0

break

;;

N)

sign=0

break

;;

*)

sign=1

continue

;;

esac

done

exit 1

fi #if 5, make sure if the user wanna overwrite the file that has been created.

cp $1 $2

if [ "$?"=0 ]

then

echo "copy successfully."

else

echo "some error(s) occured."

fi #if 6, to check if there's sth wrong.

-------------------------------------------

4. 根据下列描述,写出具体的操作步骤:

服务器(IP:10.193.251.186)上的/home/data目录下存有一部分数据。系统管理员需要添加一个新用户(user name:tmpdata)在客户端(IP:10.193.251.189),然后在客户端建立一个/home/data的挂载点(mounted),让客户端的tmpdata用户以只读权限共享服务器上/home/data 下的数据。

-------------------------------------------

#ssh root@10.193.251.186

#useradd tmpdata

#passwd ******

#ssh tmpdata@10.193.251.186

#mount -t -ro 10.193.251.186:/home/data /home/data

-------------------------------------------

5. 帮助LINUX系统管理员解决每天需要做的一些重复工作(详细如下)(hint:使用crontab)

(1)每天在4:50 p.m.删除“/abc”目录下所有的子目录和文件;

(2)每天从8:00a.m.到6:00p.m.,读取“/xyz”目录下的“x1”文件,只读取此文件的第一列,然后把读取的数据保存到“/backup”目录下的“/bak01.txt”中。(hint:cut指令)

相关文档
最新文档