QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2515|回复: 6

用c语言怎样执行shell命令???试验课遇到了困难,帮忙阿

[复制链接]
发表于 2005-5-30 22:34:38 | 显示全部楼层 |阅读模式
内容:
利用C语言编写一个微型命令解释程序,接受并解释以下命令:
⑴ dir 列当前目录
⑵ cop 文件1 文件2 拷贝文件
⑶ era文件名 删除文件
⑷ dis字符串 显示字符串
⑸ end 结束,退出

要求:
⑴ 进行命令合法性检查,若不合法,显示出错信息,等待重新输入;
⑵ 命令前后有空格为合法命令。
---------------------------------------------------------------------------------
比较字符串之类的函数我都会。
这里有个system()系统调用,当字符串匹配命令时,执行shell命令字,这个函数该怎么写。另外,是不是要把每个shell都写出来???
发表于 2005-5-31 13:26:31 | 显示全部楼层
[quote:cfdfcbdb32="蔡学镛在《从美丽的菲奥莉娜说起》中"]... 软件公司内部从事 Coding 的女性比例很低,这到底是为什么呢?我认为原因可能有二:

 1. 传统的观念认为女生不适合读自然组,所以信息系的女生很少,一班通常只有 0~5 个。

 2. 偏偏这 0~5 个女生又常常被班上当成宝,所以不太需要写程序,男生常常都会主动捉刀帮忙。

  这两个原因结合在一起就严重了。

  况且,许多女生一直都有错误的观念,认为自己「逻辑观念又没男生强」,所以「程序作业当然是找男生帮忙」,也因此信息系的男学生都很抢手。大一时女生多的科系喜欢找男生当学伴(或称学友),因为他们认为男生可以帮他们写计算器概论的程序作业。没有学伴,或者学伴自己也写不出来,女生只好上网求救。这些求救的 post 分为几种风格:

·  比较古典的标题「小女子有难,请程序高手拔刀相助」

·  比较新潮的标题「美眉需要各位葛格帮忙写一个程序」

·  比较淑女的标题「请各位大哥帮小妹解决程序问题好吗」

·  比较情色的标题「需要有人帮忙写程序,我愿意以身相许」

·  比较怨天的标题「我是女生,所以不会写程序,请帮帮忙」

·  比较尤人的标题「老师出的程序好难,害人家都不会写」

  不管标题风格为何,这些女生无非是来要作业的,绝对不是自动自发练习时遇到问题为求甚解而主动发问。[/quote]
如果你写错了,拿来让别人改是一回事;
直接一头雾水的问别人“该怎么写”又是另一回事了。
回复

使用道具 举报

 楼主| 发表于 2005-5-31 22:36:59 | 显示全部楼层
我不是女生,老大
给各切入点嘛,我是新手,现在感到没办法动手了
用c写的程序毕竟不是脚本,怎么执行linux命令呢??
给我举个最简单的例子:
用c执行ls,我自己研究
回复

使用道具 举报

发表于 2005-5-31 23:28:30 | 显示全部楼层
c 里面不是有个  system 什么的函数  可以调用外部命令的吗?

还有 linux自己的api 肯定有ls最简单功能的函数的
回复

使用道具 举报

发表于 2005-6-1 12:01:19 | 显示全部楼层
ok_snail, 既然你都知道是“有个system()调用”了,那就去看看 system() 函数的说明啊。
[code:1]$man 3 system

SYSTEM(3)                  Linux Programmer's Manual                 SYSTEM(3)



NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION
       system()  executes a command specified in command by calling /bin/sh -c
       command, and returns after the command has been completed.  During exe-
       cution  of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
       will be ignored.

RETURN VALUE
       The value returned is -1 on error (e.g. fork failed),  and  the  return
       status  of  the command otherwise.  This latter return status is in the
       format specified in wait(2).  Thus, the exit code of the  command  will
       be  WEXITSTATUS(status).   In  case  /bin/sh could not be executed, the
       exit status will be that of a command that does exit(127).

       If the value of command is NULL, system() returns nonzero if the  shell
       is available, and zero if not.

       system() does not affect the wait status of any other children.

CONFORMING TO
       ANSI C, POSIX.2, BSD 4.3

NOTES
       If  the  _XOPEN_SOURCE  feature  test macro is defined, then the macros
       described in wait(2) (WEXITSTATUS(),  etc.)  are  made  available  when
       including <stdlib.h>.

       As  mentioned, system() ignores SIGINT and SIGQUIT.  This may make pro-
       grams that call it from a loop uninterruptable, unless they  take  care
       themselves to check the exit status of the child. E.g.

           while(something) {
               int ret = system("foo");

               if (WIFSIGNALED(ret) &&
                   (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
                       break;
           }

       Do  not use system() from a program with set-UID or set-GID privileges,
       because strange values for some environment variables might be used  to
       subvert system integrity.  Use the exec(3) family of functions instead,
       but not execlp(3) or execvp(3).  system() will not, in fact, work prop-
       erly  from  programs  with  set-UID or set-GID privileges on systems on
       which /bin/sh is bash version 2,  since  bash  2  drops  privileges  on
       startup.   (Debian  uses  a  modified  bash which does not do this when
       invoked as sh.)

       In versions of glibc before 2.1.3, the check for  the  availability  of
       /bin/sh  was not actually performed if command was NULL; instead it was
       always assumed to be available, and system() always returned 1 in  this
       case.   Since glibc 2.1.3, this check is performed because, even though
       POSIX.1-2001 requires a conforming implementation to provide  a  shell,
       that  shell  may  not be available or executable if the calling program
       has  previously  called  chroot(2)   (which   is   not   specified   by
       POSIX.1-2001).

       It is possible for the shell command to return 127, so that code is not
       a sure indication that the execve() call failed.

SEE ALSO
       sh(1), signal(2), wait(2), exec(3)



                                  2004-12-20                         SYSTEM(3)

[/code:1]
回复

使用道具 举报

 楼主| 发表于 2005-6-2 18:34:39 | 显示全部楼层
我编了一个调用system()的程序。
执行了以后每次显示都是segmentation fault!!
源程序如下:


#include <string.h>

const char op1[]="dir";  
const char op2[]="dis";  
const char op3[]="cop";
const char op4[]="era";
const char op5[]="en";  
char str[];
int main()
{       
        do{
        char sp1[]="ls -l";
        char sp2[]="echo";
        char sp3[]="cp";
        char sp4[]="rm -f";      
       
        gets(str);
               if(*strstr(str,op1)!='\0')
                system(sp1);
        else if(*strstr(str,op2)!='\0')
        {       
                strcat(sp2,strstr(str,op2)+3);
                system(sp2);
        }
        else if(*strstr(str,op3)!='\0')
        {       
                strcat(sp3,strstr(str,op3)+3);
                system(sp3);
        }
        else if(*strstr(str,op4)!='\0')
        {       
                strcat(sp4,strstr(str,op4)+3);
                system(sp4);
        }
        else if(*strstr(str,op5)!='\0')
                break;
        else printf("Unknown Command!!");       
        }//while
        while(1);
return 1;
}
回复

使用道具 举报

 楼主| 发表于 2005-6-2 18:36:14 | 显示全部楼层
功能不是很正确
只要遇见命令符就执行system,这与题意不符,不过我也尽力了。
帮我看看,到底什么错了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-10-3 06:31 , Processed in 0.043126 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

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