zoukankan      html  css  js  c++  java
  • Linux基础——调用系统命令

    在这里,我们根据输入的相应命令,调用系统的命令。若不是系统的命令,自动清空该命令,否则,根据系统命令执行。

    实现代码如下:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <sys/types.h>
     4 #include <sys/wait.h>
     5 #include <unistd.h>
     6 #include <string.h>
     7 #define M '>'
     8 void word_search(char *line,char *word[1024]);
     9 int main(int argc,char *argv[])
    10 {
    11     char line[1024];
    12     char *word[1024];
    13     while(memset(line,0,1024),printf("%c",M),fgets(line,1024,stdin))
    14     {    
    15         if(line[0]=='
    ')
    16             continue;
    17         line[strlen(line)-1]='';
    18         memset(word,0,sizeof(word));
    19         word_search(line,word);
    20         if(word[0]==NULL)
    21             continue;
    22         if(fork() == 0)
    23         {
    24             execvp(word[0],word);
    25             continue;
    26         }
    27         else
    28             wait(NULL);
    29     }
    30     return 0;
    31 }
    32 void word_search(char *line,char *word[1024])
    33 {
    34     int cnt=0,bg=0,end;
    35     while(line[bg] != '')
    36     {
    37         while(line[bg] == ' ' && line[bg]!='')
    38             bg++;
    39         if(line[bg]=='')
    40             break;
    41         end=bg;
    42         while(line[end]!=' '&& line[end]!='')
    43             end++;
    44         word[cnt]=(char *)calloc(1,(end-bg+1));
    45         strncpy(word[cnt],line+bg,end-bg);
    46         cnt++;
    47         bg=end;
    48     }
    49     word[cnt]=NULL;
    50 }
    View Code

    在代码中,execvp函数会从PATH 环境变量所指的目录中查找符合参数file 的文件名,

    找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。

  • 相关阅读:
    Vue路由配置
    vue项目 favicon.ico不显示解决方案
    Vux使用AjaxPlugin发送请求跨域问题解决
    改变vux样式,自定义样式
    Hive LLAP
    GitHub提交代码无contributions记录解决办法
    Java websocket+ nginx+redis负载均衡上实现单对单通讯
    springboot异步接口
    C#中$的用法(转载)
    vmware-tools灰色而无法安装的问题
  • 原文地址:https://www.cnblogs.com/gjn135120/p/4009278.html
Copyright © 2011-2022 走看看