(Linux实验报告汇总).doc_第1页
(Linux实验报告汇总).doc_第2页
(Linux实验报告汇总).doc_第3页
(Linux实验报告汇总).doc_第4页
(Linux实验报告汇总).doc_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

(一)Shell编程一、实验目的:1)掌握在Linux下的C编程基本方法。2)掌握shell编程方法。3)掌握dialog图形化编程方法。二、 实验内容1、 编写能输出“Hello world!”问候语的C程序,并在终端中编译、执行。要求记录所使用的命令及结果。#include stdio.hmain()printf(Hello world!n);2、编写一个C程序并设置其在后台执行,其功能是在一段时间后(可自行设置),在屏幕上显示信息:Time for play!,写出相应的程序、命令及结果。#include stdio.hmain()int time=0;printf(请输入等待时间(单位:s):);scanf(%d,&time);sleep(time);printf(Time for play!n);3、编写C程序,求1到100之间整数的阶乘和,并对程序进行优化。写出程序、命令和结果。#include stdio.hmain( ) int i; double s = 1,sum = 0; for( i= 1;i= 100;i+) sum+=s*=i; printf( 1到100之间整数的阶乘和:%fn,sum); printf( 1到100之间整数的阶乘和:%en,sum); 4、编写C程序,根据键盘输入的半径求圆面积,要求在命令行使用不同的圆周率(PI=3.14,PI=3.14159,PI=3.14159626等)进行编译,写出程序、命令和结果。 #include stdio.hint main()double r = 0.0 , Area = 0.0;printf(请输入半径: );scanf(%lf, &r);Area = PI * r * r;printf(圆面积: %fn, Area); 5、编写shell程序sh.1, 完成向用户输出“你好!”的问候语。并根据实验的时间,分别给出:“上午好!”或者“下午”,或“晚上”好的问候。#!/bin/shTIME=$(date +%H) if $TIME -ge 1 & $TIME -le 11 ;then echo 早上好!elif $TIME -ge 12 & $TIME -le 18 ;then echo 下午好!elif $TIME -ge 19 & $TIME -le 24 ;then echo 晚上好!fi6、编程sh.2:如果存在sh.1文件,则输出信息:sh.1文件已经存在,并询问是否运行?如果用户回答:要运行。那么结果是什么?如果sh.1不存在,则提示用户先创建文件。#!/bin/shif -f sh.1 ; then echo 文件已经存在,并询问是否运行? now? Y/Nread aif $a = Y | $a = y ; then./sh.1elif $a = N | $a = n ; thenexit 2fielseecho sh.1不存在,先创建文件! exit 1#fi7、编程sh.3:循环显示所有的sh.*文件。#!/bin/shfor i in $(ls sh.*)dols -l $idone8、教材第9页静态库实验,按步骤完成操作,并记录结果。分析所出现的问题及如何解决的?#include void bill(char *arg)printf(bill: We passed %sn,arg);#include void fred(int arg)printf(fred: We passed %dn,arg);#include #include lib.hint main() bill(Hello World); exit(0);/* * This is lib.h.It declares the functions fred and bill for users */void bill(char *);void fred(int);#include #include lib.hint main()bill(Hello World);exit(0);9、教材第41页实验,从函数返回一个值。#!/bin/shyes_or_no()echo Is your name $* ?while truedoecho -n Enter yes or no :read xcase $x iny | yes ) return 0;n | no ) return 1;* ) echo Answer yes or noesacdoneecho Original parameters are $*if yes_or_no $1thenecho Hi $1, nice nameelseecho Never mingfiexit 010、Dialog 工具编程sh.4:实现用信息框+输入框:显示问候语“某同学,你好!”的信息。(提示:输入信息请用英文。)#!/bin/shdialog -inputbox 请输入名字 : 9 28 2name.txtNAME=$(cat name.txt)dialog -infobox $NAME同学,你好! 9 2811、设计一个图形化的菜单查询系统sh.5,标题名为“欢迎来到学生之家!”,调查内容包括:姓名、性别、年龄和建议(提示:输入信息请用英文。)。并将所得到的结果,用信息框显示出来。(格式不限,自由发挥。)#!/bin/shdialog -title 欢迎来到学生之家! -inputbox 姓名 : 10 25 -inputbox 性别 : 10 25 -inputbox 年龄 : 10 25 -inputbox 建议 : 10 25 2info.txtset $(cat info.txt)dialog -title 欢迎来到学生之家! -infobox 姓名 : $1 性别 : $2 年龄 : $3 建议 : $4 10 30三、实验结果与讨论(根据实验结果回答下列问题)1、 你所使用的实验环境是什么?请写出Linux的平台、内核版本号。2、 在Linux中,标准设备文件有哪些?这些设备文件在哪个目录? Linux 中的设备类型:字符设备(无缓冲且只能顺序存取)、块设备(有缓冲且可以随机存取),套接字设备设备文件目录:在/dev下3、 对于上述各编程题目中所用到了的各个头文件,请找到它们的位置。 C库文件在/usr/include 下四、总结你在实验遇到什么问题?如何解决的?如果没有问题那么有什么体会或看法?1 做1-100的阶乘时,结果用long int和int定义,会出现溢出,要用double类型;2 求圆面积时,本想直接定义但考虑的圆周率的精度问题,我选择了手动输入;3 执行文件如果出现权限不足,要修改权限;4 vim 是vi的升级版本,它不仅兼容vi的所有指令,还有一些新的特性,如:多级撤消,在vi里,按 u只能撤消上次命令,而在vim里可以无限制的撤消;5 由于Linux没有默认装dialog,所以我们要先装了才能用。先到Linux中找Packages文件夹中的dialog-1.1-9.20080819.1.el6.i686.rpm,然后放到虚拟机中安装;6 第8题中要加上头文件#include,就不会出现上面的警告了。 (二)Linux环境及文件操作编程一、实验目的:1)掌握库函数的制作与安装2)掌握文件操作编程基本方法。3)熟悉Linux环境设置。三、 实验内容 1、应用底层函数read( ), open( ), write( )函数,分别编写具有打开C源程序(*.C)的my_open( )函数、并能从指定文件复制文件内容的my_copy( )函数, 要求:1)编写my_open.c和my_copy.c源程序- my_open.c- #include #include void my_open(const char *path) open(path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IXUSR);- my_copy.c- void my_copy(int in, int out) char c;while(read(in, &c, 1) = 1) write(out, &c, 1);2) 再编译成my_open.o 和my_copy.o2、将上题所得到的两个目标代码,用ar程序打包成库文件名为libmy_copy.a,放在当前目录,编写能调用库函数的源程序my_prog.c,编译并运行。记录命令和并分析运行结果。#include int main() char path147,path247,c; int choice,in,out; while(1) printf(Input your choice:n); printf(1.Open file.n); printf(2.Copy file.n); printf(3.Exitn); scanf(%d, &choice); if(choice = 1) printf(Input file path and name: ); scanf(%s, path1);in= my_open(path1);while(read(in, &c, 1) = 1) write(1, &c, 1); else if(choice = 2) printf(Input oldcopy path and newcopy path: ); scanf(%s %s, path1, path2);in=my_open(path1);out=my_open(path2);my_copy(in, out); else if(choice = 3) break; else printf(Error: Please input right number.n); 3、编写教材第87页与第98页的复制程序,记录运行结果,并比较这两个程序的运行时间。-copy_system.c-#include #include #include #include int main()char c;int in, out ;in = open(file.in, O_RDONLY);out = open(file.out,O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);while(read(in,&c,1) = 1)write(out,&c,1);exit(0);-copy_stdio.c-#include #include int main()int c;FILE *in,*out;in = fopen(file.in,r);out = fopen(file.out,w);while(c =fgetc(in) != EOF)fputc(c,out);exit(0);4、编写教材第103-105页的目录扫描程序。#include #include #include #include #include #include void printdir(char *dir, int depth) DIR *dp; struct dirent *entry; struct stat statbuf; if(dp = opendir(dir) = NULL) fprintf(stderr,cannot open directory: %sn, dir); return; chdir(dir); while(entry = readdir(dp) != NULL) lstat(entry-d_name, &statbuf); if(S_ISDIR(statbuf.st_mode) if(strcmp(.,entry-d_name) = 0 | strcmp(.,entry-d_name) = 0) continue; printf(%*s%s/n,depth,entry-d_name); printdir(entry-d_name, depth+4); else printf(%*s%sn,depth,entry-d_name); chdir(.); closedir(dp); int main() printf(Directory scan of /home:n); printdir(/home,0); printf(done.n); exit(0); 5、教材第128页strftime( )函数与strptime( )函数编程,记录运行结果并与date命令的输出作比较。 #define _XOPEN_SOURCE#include #include #include int main() struct tm *tm_ptr, timestruct; time_t the_time; char buf256; char *result; (void) time(&the_time); tm_ptr = localtime(&the_time); strftime(buf,256, %A %d %B, %I:%S %P, tm_ptr); printf(strftime gives: %sn, buf); strcpy(buf, Thu 26 July 2007, 17:53 will do fine); printf(calling strptime with: %sn, buf); tm_ptr = ×truct; result = strptime(buf, %a %d %b %Y, %R, tm_ptr); printf(strptime consumed up to: %sn,result); printf(strptime gives:n); printf(date: %02d/%02d/%02dn); printf(date:%02d/%02d/%02dn,tm_ptr-tm_year%100,tm_ptr-tm_mon+1, tm_ptr-tm_mday); printf(time: %02d:%02dn,tm_ptr-tm_hour, tm_ptr-tm_min); exit(0); *6、教材第168页终端控制程序。(*可选做)三、实验结果与讨论(根据实验结果回答下列问题)4、 标准与非标准库函数有什么区别? 标准库函数:libc.a它是标准C库函数,gcc等编译程序能够自动链接,在使用时,只需要包含其定义的头文件即可;非标准库函数:在使用时,必须要包含头文件外,还要加上链接库函数的操作。5、 在Linux中,用户帐号文件在哪个目录?加密后的密码文件在哪个目录?用户帐号文件目录:/home/加密后的密码文件目录:/etc/shadow四、总结你在实验遇到什么问题?如何解决的?如果没有问题那么有什么体会或看法?1. 第五题中的警告,加个#include 头文件就不会有警告了。 (三)MYSQL数据库与Linux程序开发一、实验目的:1)掌握MYSQL数据库命令与应用编程。2)了解Linux多模块软件编译与链接过程。四、 实验内容1、 在你的Linux环境中安装MYSQL数据库系统。记录安装步骤和命令过程。答:解压mysql-5.0.87-linux-i686-glibc23.tar.zip到/usr/local;tar vxf mysql-5.0.87-linux-i686-glibc23.tar#groupaddmysql #useradd-gmysql-s/bin/false-Mmysql #cd/usr/local/mysql#./configure -prefix=/usr/local/mysql -enable-thread-safe-client -enable-local-infile -with-charset=gbk -with-extra-charset=all -with-low-memory# cp support-files/f /etc/f #bin/mysql_install_db-user=mysql#chown-Rroot. #chown-Rmysql/var #bin/mysqld_safe-user=mysql& #cp support-files/mysql.server /etc/rc.d/init.d/mysql #chmod700/etc/rc.d/init.d/mysqld #cd#chown -R mysql /usr/local/mysql/data. #chgrp -R mysql /usr/local/mysql加入自动启动服务队列: #chkconfig-addmysql #chkconfig-level345mysqlon 测试#/usr/local/mysql/bin/mysqladminping #/usr/local/mysql/bin/mysqladminversion#/usr/local/mysql/bin/mysql添加root密码 #/usr/local/mysql/bin/mysqladmin-uroot-p旧密码password新密码 说明:此时mysql的root用户的密码为空2、 编辑教材第298页select4.c程序,记录编译命令和运行结果。create table children (childno int (11) NOT NULL auto_increment,fname varchar(30),age int(11),PRIMARY KEY (childno) );insert into children(childno, fname, age) values(1,Jenny,21);insert into children(childno, fname, age) values(2,Andrew,17);insert into children(childno, fname, age) values(3,Gavin,8);insert into children(childno, fname, age) values(4,Duncan,6);insert into children(childno, fname, age) values(5,Emma,4);insert into children(childno, fname, age) values(6,Alex,15);insert into children(childno, fname, age) values(7,Adrian,9);/*select4.c*/#include #include #include mysql.hMYSQL my_connection;MYSQL_RES *res_ptr;MYSQL_ROW sqlrow;void display_header();void display_row();int main(int argc,char *argv)int res;int first_row =1;mysql_init(&my_connection);if(mysql_real_connect(&my_connection,localhost,rick,secret,foo,0,NULL,0)printf(Connection success!n);res=mysql_query(&my_connection,SELECT childno,fname,age FROM children WHERE age5);if(res)fprintf(stderr,SELECT error:%sn,mysql_error(&my_connection);elseres_ptr = mysql_use_result(&my_connection);if(res_ptr)while(sqlrow=mysql_fetch_row(res_ptr)if(first_row)display_header();first_row=0;display_row();if(mysql_errno(&my_connection)fprintf(stderr,Retrive error:%sn,mysql_error(&my_connection);mysql_free_result(res_ptr);mysql_close(&my_connection);elsefprintf(stderr,Connection failedn);if(mysql_errno(&my_connection)fprintf(stderr,Connection error %d: %sn,mysql_errno(&my_connection),mysql_error(&my_connection);return EXIT_SUCCESS;void display_header()MYSQL_FIELD *field_ptr;printf(Column details:n);while(field_ptr = mysql_fetch_field(res_ptr) != NULL)printf(t Name:%sn,field_ptr-name);printf(t Type:);if(IS_NUM(field_ptr-type)printf(Numeric fieldn);elseswitch(field_ptr-type)case FIELD_TYPE_VAR_STRING:printf(VARCHARn);break;case FIELD_TYPE_LONG:printf(LONGn);break;default:printf(Type is %d,check in mysql_com.hn,field_ptr-type);printf(t Max width %ldn,field_ptr-length);if(field_ptr-flags & AUTO_INCREMENT_FLAG)printf(t Auto incrementsn);printf(n);void display_row()unsigned int field_count;field_count =0;while(field_count mysql_field_count(&my_connection)if(sqlrowfield_count)printf(%s ,sqlrowfield_count);elseprintf(NULL);field_count+;printf(n);3、 编写程序p.c,其功能是从键盘输入两个实数,输出这两个实数的平方和,生成可执行文件为pow。编写源程序,记录编译的命令和结果。#include #includemath.h int main() double x,y,z; printf(Please enter the two real Numbers:n); scanf(%lf,%lf,&x,&y); z =pow(x,2)+pow(y,2); printf(Sum of squares:n ); printf(%lfn,z); 4、 编程power.c,其功能是从键盘输入两个实数x和y,计算x的y次方,生成可执行文件名为XpowerY。编写源程序,记录编译命令及程序的运行结果。#include #includemath.h int main() double x,y,z; printf(Please enter the two real Numbers:n); scanf(%lf,%lf,&x,&y); z =pow(x,y); printf(Result:n ); printf(%lfn,z); 5、 完成P319页的实验过程,创建相关的文件,并记录命令和结果。/*main.c*/#include #include a.hextern void function_two();extern void function_three();int main()function_two();function_three();exit (EXIT_SUCCESS);/*2.c*/#include a.h#include b.hvoid function_two()/*3.c*/#include b.h#include c.hvoid function_three()6、 完成P327页管理函数库的实验,并将库文件名改为libmy.a, 创建后将其安装到系统的库文件默认目录。1) 完成P327的所有步骤;all: myappCC= gcc INSTDIR = /usr/local/binINCLUDE = .CFLAGS = -g -Wall -ansiMYLIB = mylib.amyapp: main.o $(MYLIB)$(CC) -o myapp main.o $(MYLIB)$(MYLIB): $(MYLIB)(2.o) $(MYLIB)(3.o)main.o: main.c a.h 2.o: 2.c a.h b.h 3.o: 3.c b.h c.hclean:-rm main.o 2.o 3.o $(MYLIB)install: myapp if -d $(INSTDIR); then cp myapp $(INSTDIR); chmod a+x $(INSTDIR)/myapp; chmod og-w $(INSTDIR)/myapp; echo Installed in $(INSTDIR); else echo Sorry,$(INSTDIR) does not exist; fi2) 安装库文件,并写出安装库函数文件的步骤;3)写出使用所创建库libmy.a的命令。 /*main.c*/#include #include #include a.hvoid function_two()printf(function_two() n);void function_three()printf(function_three() n);int main()function_two();function_three();exit (EXIT_SUCCESS);三、实验结果与讨论(根据实验结果回答下列问题)1、写出使用C访问MYSQL数据库的步骤。你

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论