`
LINUX系统与程序设计 大作业(2012年春)
1.Shell编程
查询系统最近1000条命令中使用最多的前10条命令,编写Shell脚本实现此功能。写出具体代码和操作过程。(提示:history,管道符号|)
Query the top 10 commands, which system has used in the latest 1000 commands. Write shells script to realize this function. Please provide specific code and operation process (hint: history, |).
Shell脚本:
echo $HISTSIZE 1000
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] \\CMD[a]/count*100 \head -n10
解释:第一行的echo $HISTSIZE 1000是设置history中记录1000条命令;第二行中用 awk 统计 history 中的命令部分出现的次数,去掉 “./\这样不算命令的情况,最后排序输出,列出前 10 个负责统计最近1000条命令中使用最多的10条命令。
注:history在脚本中运行可能不能正常输出结果,需要用“source+脚本文件名”运行。
编写shell脚本删除当前目录下空Log文件,并且打印删除文件数目。
Shell programming: remove all the empty .log files in your current directory and print the number of removed files. Shell脚本: #!/bin/bash
find ./ -size 0 –name “.log” | wc –l
find ./ -size 0 –name “.*.log” –exec rm –f ?{}? \\;
第一个find命令显示当前目录下空log文件的数目,第二个find删除这些文件。
2. CPU info
Linux操作系统的系统配置文件默认储存在/etc目录下,因此管理员可以通过查看相应的文件来得知系统配置信息。编写一个shell脚本显示下列系统配置信息: (1)CPU相关信息:如处理器类型,速度等; (2)内存信息; (3)挂载文件信息。
Most of the system configuration files of Linux OS system (Red Hat e.g.) are in the /etc directory unless other specified. So you can get the system configuration information by reading those files. Please write a shell script to show following system configuration information:
1. CPU information like processor type, speed etc. 2. Memory information. 3. File system (Mounted). 首先要说明的是,Linux操作系统的大部分配置文件储存在/etc目录下,但大部分系统信息却存储在/proc目录下,题目中要求的很多信息在/etc目录下是找不到的。 Shell脚本:
`
#!/bin/bash echo CPU
cat /proc/cpuinfo echo 内存
cat /proc/meminfo echo 挂载文件 cat /proc/mounts
三个cat命令分别显示CPU、内存和挂载文件信息。 注:(1)按上面的脚本执行后,CPU显示信息较杂,若只想查看型号和主频,第一个cat命令可用如下命令替换:
cat /proc/cpuinfo | grep name | cut –f2 –d: | uniq –c
(2)挂载文件信息在/etc目录下也存在,分别是fstab和mtab文件,对应系统中的静态和动态挂载信息,但它们的信息均不如/proc/mounts文件全。
3. NFS Mount
根据下列描述,写出具体的操作步骤:
服务器(IP:10.193.251.186)上的/home/data目录下存有一部分数据。系统管理员需要添加一个新用户(user name:tmpdata)在客户端(IP:10.193.251.189),然后在客户端建立一个/home/data的挂载点(mounted),让客户端的tmpdata用户以只读权限共享服务器上/home/data下的数据。 操作步骤:
首先,为了使客户端(10.193.251.189)上的用户tmpuser对共享目录有读写权限,在服务器(10.193.251.186)上以root身份进行如下设置:
(1)执行命令setfacl –m u:tmpuser:rwx /home/test
(2)编辑/etc下的exports文件,加上选项/home/data 10.193.251.189(rw) 然后在客户机上以root身份执行如下操作: (1) 创建用户:
useradd tmpdata passwd tmpdata
(2) 创建挂载点目录:
mkdir /home/mnt
(3) 挂载:
mount –t nfs 10.193.251.186:/home/data /home/mnt
4.Samba Service
根据下列描述,写出具体的操作步骤:
Samba允许Windows机器访问Linux机器。
1. 在Linux服务器端创建新的Linux账户tmpuser,登录密码为“tmpuser”;tmpuser的主
目录锁定为home/tmpuser;
2. 添加tmpuser为新的samba用户; 3. 配置samba服务,将/home/tmpuser目录作为samba的共享目录,允许tmpuser用户可写,
服务器上的其它用户tmpuser1和tmpuser2只读(假设tmpuser1和tmpuser2是已经存在的samba用户);
`
4. 在Windows端测试配置是否成功。 Linux服务器IP地址:10.193.251.186。
Samba service allows Linux server to communicate with clients of Windows operation system. 1. Add a new Linux user “tmpuser”, password: “tmpuser”; the directory of this user is
“/home/tmpuser”.
2. Make user “tmpuser” to be a samba user
3. Make “/home/tmpuser” to be the samba directory, and “tmpuser” can write on the directory
while others just read only.
4. Test that whether the samba service works or not. The IP address of Linux server: 10.193.251.186
There already exist two samba user “tmpuser1” and “tmpuser2”.
操作步骤:
(1) 在Linux服务器端添加用户tmpuser:
useradd tmpuser passwd tmpuser
(2) 更改/home/tmpuser的所有者和所属用户组:
chown –R tmpuser /home/tmpuser chgrp –R tmpuser /home/tmpuser
(3) 添加tmpuser为新的samba用户:
smbpasswd –a tmpuser
(由题意,tmpuser1和tmpuser2为已存在的samba用户,因此不用对它们进行设置)
(4) 编辑samba配置文件:
该文件为/etc/samba/smb.conf,为保险建议先备份,再新建一个空smb.conf文件,添加如下内容:
[global]
workgroup=samba //工作组名
server string=samba server //服务器的描述 security=user //需要输入用户名和密码
[shared]
comment=/home/tmpuser //描述
path=/home/tmpuser //服务器上的共享目录
public=no //其它用户(非write list和valid user)无法看到此目录 read only=no //是可写的
write list=tmpuser //tmpuser有可读写权限
valid user=tmpuser1,tmpuser2 //tmpuser1,tmpuser2只有只读权限 (5) 重启samba服务
/etc/init.d/smb restart或者service smb restart
(6) 测试
打开Windows资源管理器,在地址栏里输入“\\\\10.193.251.186”,输入用户名和密码即可测试,为确保测试成功,Linux的防火墙一定要关闭。
5.Crontab
`
帮助LINUX系统管理员解决每天需要做的一些重复工作(详细如下)(hint:使用crontab) (1)每天在4:50 p.m.删除“/abc”目录下所有的子目录和文件; (2)每天从8:00a.m.到6:00p.m.,读取“/xyz”目录下的“x1”文件,只读取此文件的第一列,然后把读取的数据保存到“/backup”目录下的“/bak01.txt”中。(hint:cut指令)
(3)在每个星期一的5:50p.m.,把“/data/”中的所有目录和文件压缩存档到一个文件“backup.tar.gz”。
(4)每天5:55p.m.卸载CD-ROM。(假设CD-ROM是被命名为hdc) 操作步骤:
(1) 终端窗口下执行命令crontab –e,使用vim编辑计划任务; (2) 在vim窗口中,添加如下几项: 50 16 * * * rm -rf /abc
* 8-18 * * * cut -d: -f 1 /xyz/x1 > /backup/bak01.txt 50 17 * * mon tar -czf backup.tar.gz /data 55 17 * * * umount /dev/hdc
6.gcc,Makefile
Makefile与C/C++编程:本题考查Makefile的编写,C语言基础,以及编程规范性。编写C/C++程序,完成:给定一个m行n列的矩阵A,和一个n行k列的矩阵B,结果输出矩阵相乘的结果,即A*B。
输入文件:in1.file 第一行是m和n 3 3 1 2 1 2 3 1 1 3 2
输入文件:in2.file 第一行是m和k 3 1 2 1 1
输出文件:out.file 5 8 7
要求:(1)建立四个文件:main.c function.c function.h Makefile; (2)用C/C++语言实现,main.c仅包含main()函数,负责输入字符串,调用转换函数。 在合适的文件中声明你的数据结构和函数,在合适的文件中实现转换函数。
(3)编写Makefile,并使用G++通过Makefile对你写的程序实现编译,连接,形成最终可以执行的文件。
对不满足程序规范性的答案酌情扣分。(考点:C/C++语言中文件读取,动态二维数组建立) Write an ANSI C code. Use a Makefile to build it (including compile and link) into binary file. The function of this binary file should be: Given a file containing matrix A and a file containing matrix B, you are supposed to print the result A*B.
`
Input file: in1.file 第一行是m和n 3 3 1 2 1 2 3 1 1 3 2
Input file: in2.file 第一行是m和k 3 1 2 1 1
output: out.file 5 8 7
You should give:
1. Makefile (hint: four files, main.c, function.c, function.h, makefile).
2. All C source code. main.c only passes parameter of input output and calling function from
function.c.
3. The whole procedure of how you compile and link C code.
function.c文件
#include \
int **A,**B,**C; int m,n,n1,i,j,l,k;
void MatrixMutiply() {
for(i = 0; i < m; i++) {
for(j = 0; j < k; j++) {
for(l = 0; l < n; l++) {
C[i][j] += A[i][l] * B[l][j]; } } } }
int** Create(int a, int b) {
int **Matrix;
Matrix = (int **) malloc(sizeof(int *) * a);
百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典教育范文LINUX大作业(北邮信息工程2024年春)在线全文阅读。
相关推荐: