LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 934|回复: 8

求教,unix编程高手请进…… 

[复制链接]
发表于 2003-12-18 12:28:59 | 显示全部楼层 |阅读模式
我是初学者,有很多东西都不懂,现在要做几道题,请各位高手帮忙,很急……
谢谢先!


1.编写一个程序,他的 功能是完成代码“key555"的查找。
程序运行结果要求:(1)显示该程序标题(自己可随意取标签名称)
                  (2)应有找到和未找到代码“key555"的 提示信息,这些提示
                     信息都要求突显。

2.写一个程序,使其运行的结果是:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

3.编写程序,使它只接受一个命令行参数,如果此参数是目录,程序显示该目录下所有文件和子目录的数量(不包括隐藏文件)。
需要考虑以下各种情况:
①无命令行参数或者有多个命令行参数;
②命令行参数是否为存在的目录;
程序运行结果要求:
显示带下划线的 该程序标题(自己可随意取标题名称)
无论何种情况都应有提示信息。

程序运行结果如下:
$ listmore xy       ./xy目录下现有2个文件
The number of files in the directory is: 2
$

4.有时候,您所写的Script可能会跨越好几种平台,如Linux、SunOS、Solaris等等,而各平台之间,
多多少少都有不同之处,有时候需要判断目前正在那一种平台上操作。编写一个程序使之能分别针对
不同的系统执行不同的操作(如输出该系统的名称等)。
提示:找出可以判断目前所使用的系统名称的命令。

程序执行结果如下:
$syst
This system is Linux.
$

5.编写一个具有两个参数的程序,第一个参数是目录名,第二个是以字节计的文件容量。
这一命令应列出给定目录(即第一个参数)中具有读权限,而且,容量大于给定容量(即第二个参数)
的所有普通文件名。程序应检查命令行只有两个参数,而且第一个参数是目录名。否则提示用户从新输入。
运行结果如下:
$listfile . 20     列出当前目录下所有大于20的可读文件
./file1 has size 140 bytes
./file2 has size 230 bytes
./file3 has size 4560 bytes

6.编写程序inout,它的功能是从标准输入设备读入文本,将它复制到标准输出,并且在每一输出行前面加一行号。
执行结果如下:
$inout[enter]                           执行程序
what’s your name? [enter]      输入文本
1 what’s your name?               程序执行的输出
I’m Tom. [enter]                     输入文本
2 I’m Tom.                             程序执行的输出
ctrl-c                                       退出程序
$
 楼主| 发表于 2003-12-18 12:42:52 | 显示全部楼层
主要是3、5……    其它的,已经编了……
不过,没有把握……
我知道有些麻烦,不过,真的很急……
请懂的的朋友帮一下忙……
谢谢
发表于 2003-12-18 17:01:41 | 显示全部楼层
check "stat","fstat","lstat"
发表于 2003-12-18 21:59:21 | 显示全部楼层
什么叫程序标题
发表于 2003-12-20 03:09:05 | 显示全部楼层
题3,不知这个符合你的意思不:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>

  4. int
  5. main(int argc, char *argv[])
  6. {
  7.   DIR *dp;
  8.   struct dirent *dirp;
  9.   int i = 0;
  10.   
  11.   if(argc != 2){
  12.     fprintf(stderr, "usage: %s directory\n", argv[0]);
  13.     exit(-1);
  14.   }
  15.   if((dp = opendir(argv[1])) == NULL){
  16.     fprintf(stderr, "%s is not directory\n", argv[1]);
  17.     exit(-1);
  18.   }
  19.   while((dirp = readdir(dp)) != NULL)
  20.     if(dirp->d_name[0] != '.')
  21.       i++;
  22.   printf("\033[4m%s\033[0m: %s have %d items\n", argv[0], argv[1], i);

  23.   exit(0);
  24. }
复制代码
发表于 2003-12-20 03:54:19 | 显示全部楼层
看看第4题,好像用脚本也可以,我就用perl写了一个第5题的:

  1. #! /usr/bin/perl -w

  2. die "require two arguments: DIRECTORY and SIZE" if @ARGV != 2 or not -d $ARGV[0];

  3. while(<$ARGV[0]/*>){
  4.   if(-f && -r && ($size = -s) > $ARGV[1]){
  5.           print $_, "has size $size bytes\n";
  6.         }
  7. }
复制代码
发表于 2003-12-20 20:55:25 | 显示全部楼层
2题
递归法
2\

  1. int print(int i)
  2. {
  3.    int j;
  4.    if(i>0){
  5.        for(j=0;j<i;j++){
  6.           printf("%d ",j);
  7.        }
  8.    printf("\n");
  9.    i--;
  10.    print(i);
  11.    }
  12. return 0;
  13. }
复制代码

顺便写的,肯定有bug,大概这样
发表于 2003-12-21 13:23:25 | 显示全部楼层

  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <stdio.h>
  5. #include <dirent.h>
  6. #include <unistd.h>

  7. int main(int argc, char** argv)
  8. {
  9.   struct stat file_stat;
  10.   struct dirent * direntp;
  11.   DIR * dirp;
  12.   char tmp[256];
  13.   long size=0;

  14.   if(argc !=3)
  15.     {
  16.       printf("usage: %s directory size\n", argv[0]);
  17.       return -1;
  18.     }

  19.   if(access(argv[1],  R_OK ) == -1)
  20.     {
  21.       printf("%s permission denied\n", argv[1]);
  22.       return -1;
  23.     }

  24.   if(stat(argv[1], & file_stat) == -1)
  25.     {
  26.       printf("read %s error\n", argv[1]);
  27.       return -1;
  28.     }

  29.   if(S_ISDIR( file_stat.st_mode) != 1)
  30.     {
  31.       printf("%s is not a directory\n", argv[1]);
  32.       return -1;
  33.     }

  34.   if( (dirp = opendir(argv[1])) == NULL)
  35.     {
  36.       printf("open directory %s error", argv[1]);
  37.       return -1
  38.     }

  39.   size = atoi(argv[2]);
  40.   if (size < 0)
  41.     {
  42.       printf("arg 2 size error\n");
  43.       return -1;
  44.     }

  45.   while( (direntp = readdir(dirp)) != NULL )
  46.     {
  47.       if (strcmp(direntp->d_name, ".") == 0 \
  48.           || strcmp(direntp->d_name, "..") == 0 )
  49.         continue;

  50.       //      if(direntp->d_type == 4 )
  51.       //        continue;
  52.        
  53.       strncpy(tmp, argv[1], strlen(argv[1]) + 1 );

  54.       if(strcmp(tmp, "/") != 0 )
  55.         strncat(tmp, "/", 1);

  56.       strncat(tmp, direntp->d_name, strlen(direntp->d_name) );
  57.       

  58.       if(access(tmp,  R_OK ) == -1)
  59.         {
  60.           printf("file: %s permission denied\n", tmp);
  61.           continue;
  62.         }

  63.       if(stat(tmp, &file_stat) == -1 )
  64.         {
  65.           printf("file: %s, read info error\n", tmp);
  66.           continue;
  67.         }
  68.       if( S_ISDIR(file_stat.st_mode) == 1) continue;
  69.       if (file_stat.st_size >= size)
  70.         {
  71.           printf("file: %s, size=%d\n", tmp, file_stat.st_size);
  72.         }
  73.     }

  74. }




复制代码


gcc -g main.c -o main

应该可以,你再完善一下
 楼主| 发表于 2003-12-28 22:43:53 | 显示全部楼层
谢谢各位,问题已经解决……    谢谢大家……    ^_^
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表