您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > Linux环境编程习题-编程题-答案
Linux环境编程-人民邮电出版社-姜林美课后习题(编程题)答案第三章.......................................................................................................................................................................1第五章.......................................................................................................................................................................4第六章.......................................................................................................................................................................9第七章.....................................................................................................................................................................19第八章.....................................................................................................................................................................22第九章.....................................................................................................................................................................35第十章.....................................................................................................................................................................38第三章15.请编程实现:判断任一参数给出的是不是字符设备文件,如果是则将其拷贝到/dev目录下。(注:本题不妥,为方便测试可将“字符设备文件”改为“符号连接文件”,“/dev”改为“家”。相应地以下答案中的“-c”,改为“-h”,“/dev”改为“~/”)#!/bin/bashwhile[$1];doif[-c$1];thenmv$1/devfishiftdone更改后答案#!/bin/bashwhile[$1];doif[-h$1];thenmv$1~/fishiftdone16.请编写程序,分别用while、until和for循环计算从1到“位置参数$1所给出的数”之间的所有能被3整除的数之和。#!/bin/bashsum=0for((i=1;i=$1;i++));doif((i%3==0));then((sum+=i))fidoneecho$sum17.请编程实现:打印边长为n的由“*”号组成的等边三角形,形如:***************其中变量n的值通过命令行参数传入。#!/bin/bashif[-z$1];thenechoPleaseprovidedanumberexitfifor((i=1;i=$1;i++));dofor((j=0;j$1-i;j++));doecho-ndonefor((j=0;ji;j++));doecho-n*doneecho;done18.设/tmp路径下有1000个文件,文件名的格式为:filename_YYYYMMDD_xxx.dat(其中YYYYMMDD为8位数字表示的日期,xxx为三位数字表示的序列号,例如:backup_20040108_089.dat)。请编程实现:将这些文件名依次改名为YYYYMMDD_filename_yyy.dat,其中yyy=1000-xxx。例如,将backup_20040108_089.dat改名为20040108_backup_911.dat。#!/bin/bash#backup_20040108_089.dat=20040108_backup_911.datforfilein/tmp/*.dat;dof=${file#/*/}#filenameb=${f%%_*}#backupmm=${f%_*}#backup_20040108m=${mm#*_}#20040108ee=${f##*_}#089.date=${ee%.*}#089t=${e##0}#89d=$((1000-$t))#911d=`printf%03d$d`#911mv$file/tmp/${m}_${b}_${d}.datdone第五章5.请编写一个程序,在用户的主目录下创建一个文件“rect.sh”。将其权限设置为:所有者可读、可写、可执行,所属组和其他用户可读、可执行。文件内容为一脚本,当执行rect.sh后,可输出一个5*5的由1~25之间的数字组成的方形,如下所示:12345678910111213141516171819202122232425#includesys/types.h#includesys/stat.h#includefcntl.h#includeunistd.h#includepwd.h#includestdio.hintmain(void){intfd;/*shell文件内容*/charcont[]=#!/bin/bash\nfor((i=0;i5;i++));do\nfor((j=0;j5;j++));do\nx=$((i*5+j+1))\necho-n-e$x\\t\\ndone\necho\\\ndone\n;charpath[512];/*取家目录连接文件名*/sprintf(path,%s/rect.sh,getpwuid(getuid())-pw_dir);if((fd=open(path,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH))0){write(2,createerror\n,13);return-1;}if(write(fd,cont,sizeof(cont))!=sizeof(cont)){write(2,contentwriteerror\n,20);close(fd);return-1;}close(fd);return0;}6.请编写程序,将用户主目录下的所有普通文件复制到/tmp目录。#includesys/types.h#includeunistd.h#includesys/stat.h#includedirent.h#includepwd.h#includefcntl.h#includestdio.h#defineBUFSIZE4096intcopy(constchar*srcPath,constchar*filename,constchar*dstPath){intrs=0;size_tnRead,nWrite;charsrcFile[BUFSIZE],dstFile[BUFSIZE],buf[BUFSIZE];intfdIn,fdOut;if(!srcPath||!dstPath||!filename)return-1;snprintf(srcFile,BUFSIZE,%s/%s,srcPath,filename);snprintf(dstFile,BUFSIZE,%s/%s,dstPath,filename);if(access(srcFile,R_OK)!=0)return1;/*不可读文件*/if((fdIn=open(srcFile,O_RDONLY))0){return-1;}if((fdOut=open(dstFile,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH))0){close(fdIn);return-1;}while(1){nRead=read(fdIn,buf,BUFSIZE);if(nRead0)/*出错*/{rs=-1;break;}elseif(nRead==0)/*结束*/{rs=0;break;}nWrite=write(fdOut,buf,nRead);if(nWrite!=nRead){rs=-1;break;}}close(fdOut);close(fdIn);returnrs;}intmain(intargc,char*argv[]){DIR*dir;structdirent*ptr;inti=0;char*home=getpwuid(getuid())-pw_dir;/*取家目录*/if((dir=opendir(home))==NULL){write(2,opendirerror\n,14);return-1;}while((ptr=readdir(dir))!=NULL){if(ptr-d_type!=DT_REG)continue;if(copy(home,ptr-d_name,/tmp)0){write(2,copyerror\n,11);break;}}closedir(dir);return0;}7.请编写程序,在用户的主目录下创建一个到/bin/bash的硬链接文件“mysh”,再创建一个到/bin/bash的符号链接文件“bash”,然后读取/bin/sh的符号链接,最后删除前面创建的两个链接文件。(注:需要以sudo执行本程序才能创建硬连接,创建的连接文件在/root目录下)#includesys/types.h#includesys/stat.h#includeunistd.h#includepwd.h#includestdio.h#includestdlib.h#includeerrno.h#includestring.hintmain(intargc,char*argv[]){charpath_hard[512],path_sym[512],buf[512];/*取目标文件名*/sprintf(path_hard,%s/mysh,getpwuid(getuid())-pw_dir);if(link(/bin/bash,path_hard)!=0){printf(%s-%s\n,path_hard,strerror(errno));gotoERROR;}sprintf(path_sym,%s/bash,getpwuid(getuid())-pw_dir);if(symlink(bin/bash,path_sym)!=0){printf(%s-%s\n,path_sym,strerror(errno));gotoERROR;}if(readlink(/bin/sh,buf,512)0){printf(/bin/sh-%s
本文标题:Linux环境编程习题-编程题-答案
链接地址:https://www.777doc.com/doc-8143356 .html