1、6什么是vi? 其三种操作模式的含义是什么?给出三种工作模式间的转换图。命令模式:vi启动后的默认模式,控制光标的移动,复制删除文字,进入输入模式和末行模式输入模式:进行文字输入末行模式:保存文件,退出VI三、 实验内容(包含实验所用命令或相关程序源代码)1shell操作命令(给出每题所用的Shell命令或命令结果)(1)创建名为stu1、stu2的2个用户,设置密码分别为student1和student2 ,并将它们设为组group1中的成员。#groupadd group1#useradd stu1 g group1#su stu1Spasswd stu1 回车后敲入密码student1$
2、exit#useradd stud2 g group1#su stu2$passwd stu2(2)在每个用户的主目录下建立2个目录,分别命名为dir1和dir2。$cd$mkdir dir1$ cd$mkdir dir2(3)查找stu1用户主目录、下属各子目录的默认存取权限,并给出结论。$cd.$ls-1用户主目录权限为:drwx-,即目录的用户可读,写,执行,同组和其它的用户无任何权限#su stu$ls-1Dir1目录权限为:drwxr-xr-x,即目录的用户可读,写,执行(4)调试pwd和cd命令,回答下列关于Linux文件系统中目录的有关问题。 用户主目录的绝对路径如何表示? /h
3、ome/stu1/home/stu2 根目录如何表示? /root 和分别表示什么目录? 子目录,父目录 表示什么目录? 用户主目录 当前目录如何表示? Cd (5)新建用户stu3,设置其密码为student3,并将其设置为group2中成员。尔后,以stu3登录,在其主目录下建立名为text的目录,在text目录下再建立名为dir1的子目录,并使其成为当前目录。#groupadd group2#useradd stu3 g group2#su stu3$passwd 回车后敲入密码 student3$mkdir text$cd text/dir1(6)使用catsneakers.txt命令
4、,分析命令行cat sneakers.txt的显示结果。$ catsneakers.txt buy some sneakersthen go to the coffee shop then buy some coffD$cat sneakers.txt从键盘中创建一个名为sncakers.txt文件,文件内容为:buy some sncakersThen go to the coffee shopThen buy some coff(7)使用上题的方法,在dir1目录中建立home.txt文件,其内容为: bring the coffee home take off shoes put on
5、sneakers make some coffee relax!$cd /home/stu1/dir1$cathome.txtBring the coffee homeTake off shoesPut on sneakersMake some coffeeRelax!ctrl+d(8)试写出实现下列功能的shell命令: 将home.txt文件移至其上级目录中(即text目录下)。$mv/home.txt 将home.txt添加到sneakers.txt尾部形成新的文件saturday.txt。$cat cneakers.txtasturday.txt$cat home.txtSaturda
6、y.txt 将text目录树从stu3用户主目录下移至stu2主目录下。【使用特权用户方法】sumv/home/stu3/text/home/stu2【修改目录权限方法】#cp/home/stu3/text/home/stu2(9) 试画出上述命令后,用户stu1、stu2和stu3主目录中的目录树(3棵子树)。2 Linux C程序开发(1)编写Linux C程序,把一个文件的内容复制到另一个文件中,即实现简单的copy功能。要求:程序输入的第一个参数是源文件,第二个参数是目标文件。【源程序】#includedirent.hstdio.hcrrno.hInt main(int argc,ch
7、ar *argv)FILE *in,*out;Char ch;If(argc!=3)Printf(“you forgot to enter a filenamen”);Exit(0);If(in=fopen(argv1,”r”)=NULLPrintf(“can nit open outfilen”);If(out=fopen(argv2,”w”)=NULLPrntf(“can not open outfile”);Exit(0);While(!feof(in)fputc(in),out);Fclose(in);Fclose(out);【运行命令】#gcc o test copy.c#./tes
8、t file1.c file2.c(2)编写Linux C程序,列出stu2主目录中的文件信息,并将其中saturday.txt文件的权限设置为文件所有者可读可写、同组用户只读、其他用户无权限。DIR *dp;Struct dirent *dirp;Int n=0;If(arge!=2)Printf(“a signle argement is required”);If(dp=opendir(argv1)=NULL)Printf(“can not open%s”,args1);While(dirp=readdir(dp)!=NULL)&(nd_name);System(“chmod 640/h
9、ome/stu2/text/dir1/Saturday.txt”)#gcc o test list.c#./test.home/stu2 SHELL程序设计 2014 年 4 月 16 日 第 9 周 星期三熟悉SHELL脚本编程的步骤,掌握基于Bash的Shell脚本开发。1. Linux系统默认的shell语言是什么?欲查看该shell的版本,应使用什么命令?Bash shell$echo $BASH_VERSION2. 预习shell有关变量和参数的相关知识,回答下列问题。(1)假设用户进行了如下的赋值操作:$ person=jenny试给出下面命令的输出结果。1) echo perso
10、n person 2) echo $person jenny 3) echo $person $person 4) echo “$person” jenny (2)填充下列与环境变量、位置变量和预定义变量相关的表格。Shell变量定义HOME保存用户注册目录的绝对路径PATH保存用冒号分割的目录路径PWD当前工作目录的据对路径名PS1主提示符,特权用户为#,普通用户为$0当前shell程序的文件名$#位置参数的个数$?前一个命令执行后返回的状态$当前进程的PID3写出下列expr命令的输出:(1) expr index “value” a(2) expr “value” : v.*u(3) e
11、xpr “aaa” : a+(4) expr “aaa” : a?(5) expr 2+3 (6) expr 2 + 3 (7) expr 2 * 3(8) expr 5 + expr 2 + 3(9) expr length “operating system”(10) expr substr linux 2 32 4 3 1 2+3 5 6 mon-numeric argument 16 inu三、 实验内容1编写Shell脚本,从命令行中接收一个二元算术表达式并计算其结果。#!/bin/bashIftest $#=3Then Case s2 in +)let z=$1+$3;-)let
12、z=$1-$3;/)let z=$1/$3;x|x)let z=$1*$3;*)echo”warning -$2 invalid operator!” exit;EsacEcho”answer is sz”ElseEcho”usage - $0 value1 operator value2”fi【运行】Chmod a+x jisuan./jisuan 2+32编写一个以文件列表作为输入的过滤器程序,要求文件名含有以句点“.”分隔的后缀,过滤器输出每个文件的不带句点和后缀的文件名。Read flagWhile test “$flag” Do Location=expr $location=1Ba
13、sename=expr substr=”$flag” 1 $locationEcho $basecnamedoneChmod a+x letter./letter3将下面的shell脚本命名为myscript,分析其功能:count=$#cmd=echowhile $count gt 0 do cmd=$cmd $count count=expr $count - 1eval $cmd【命令行输入】 chmod a+x myscript ./myscript first second third【运行结果】Third second first【脚本功能分析】将命令行输入的参数倒叙显示4设计一个
14、程序cuts,它从标准输入读入数据,获取由第一个参数n 和第二个参数m所限定范围的数据(包括这两个字符),n和m都是整数。例如:$ cuts 11 14This is a test of cuts program (输入)test (显示结果)Read blineEcho $aline |cut-c $1-$2 进程控制与通信 2014 年 4 月 30 日 第 11周 星期三 1加深进程的概念理解,体会进程创建过程,经一部认识进程的异步并发特征2,了解Linux进程通信原理3,掌握Linux进程控制和进程通信相关的系统调用二、实验预习(预备知识的问题及回答)1写出下列系统调用功能: (1)f
15、ork()用于 创建进程 (2)getpid()用于 获取当前的进程ID号 (3)wait()用于 等待子进程结束 (4)exit()用于 进程自我终止 (5)pipe()用于 常见无名管道 (6)signal()用于 在信号和信号处理函数之间建对应关系 (7)kill()用于 发送信号给指定进程 2阅读fork系统调用,用伪码写出其实现流程。Pid=fork()If pid 为负Print 当前进程是子进程Else if pid 为0Print 当前进程是父进程3图示pipe系统调用生成无名管道时所涉及的数据结构。4. 在UNIX系统中运行下面程序,最多可以产生多少个进程?画出进程家族树。m
16、ain( ) fork( ); fork( );8个5.下列程序运行后,a的值是多少? int a,pid; a=55; pid=fork( );if(pid0) printf(errorinfork !); exit(0); else if(pid=0) sleep(5); a=99; printf(“a=%dn”,a);sleep(5); exit(0); else sleep(7); printf(“a=%dn”,a); wait(0); a=99 a=55;最终a=55三、实验内容1调试下面的程序,观察可能的并发结果,给出简要分析,并画出进程家族树。#includesys/wait.h
17、main() int status;intpid1=-1,pid2=-1,pid3=-1;pid1=fork();(pid1=0)pid1=0,myprocessidis%dn,getpid(); else 0) printf(pid10,my pid2=fork( );(pid2pid2=0, myelse 0) pid20, my pid3=fork();(pid3pid3=0,pid1=%d,pid2=%d,my,pid1,pid2,getpid();pid30, pid1=%d,pid2=%d, myis %dnwait(&status);exit(0);2编程实现进程间管道通信。 要
18、求:父子进程共享一无名管道,两个子进程作为发送方分别向管道发送一行信息,父进程先读出子进程P1发来的信息行,将其转换为大写字母输出;再读出子进程P2发来的信息行,将其转换为小写字母输出。【源程序】 Int filedes2;Char buffer80;Main()Pipe(filedes);Char str180;Char str280;Int pid1,pid2,I;While(pid=fork()=-1);If(pid1=0)Lockf(filedes1,1,0);Printf(“child1 input String1n”);Scanf(“%s”,str1);Write(filedes1
19、,str,sizeof(str1);Lockf(filedes1,0,0);ElseWhile(pid=fork()=-1)If(pid2=0)Lockf(filedes1,1,0)Printf(“child2 input string2n”);Scanf(“%s”,str2);Write(filedes1,str2,sizeof(stru2);Lockf(fildes1,0,0);If(waitpid(pid1,null,0)=pid1)Read(filedes0,buffer,80);For(i=0;isizeof(str1);i+)bufferi=toupper(bufferi);Printf(“parent=child1:%sn”,buffer);Print
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2