用管道运行命令 [英] Running commands with pipes

查看:72
本文介绍了用管道运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个shell,其中子进程运行linux命令(在execvp的帮助下),如ls等。问题是我还希望它支持管道命令,如ls / tmp | wc -l。我现在的程序适用于像ls或ls -l / tmp这样的命令

解决方案

对于 dup2 您可以在这里查看: dup2(2) - Linux手册页 [< a href =http://linux.die.net/man/2/dup2target =_ blanktitle =New Window> ^ ]



对于代码,从外部循环开始,在此处将输入字符串拆分为管道字符。然后创建一个负责执行每个命令的函数。

  #include     stdafx.h 
#include < stdio.h >
#include < string.h >
#include < ctype.h >
#include < malloc.h >

size_t TrimWhiteSpace( char * out, size_t len, const char * str);
void 执行命令( const char *命令);

int main()
{
char 输入[ 80 ] = ls / tmp | wc - 升;
const char sep [ 2 ] = |;
char * token = NULL;
char * command = NULL;

token = strtok(input,sep);

while (token!= NULL)
{
command =( char *)calloc(strlen(token)+ 1 sizeof ));
TrimWhiteSpace(命令,strlen(标记)+ 1 ,标记);

printf( %s \ n,command);

ExecuteCommand(command);

token = strtok(NULL,sep);
}
return 0 ;
}

size_t TrimWhiteSpace( char * out, size_t len, const char * str)
{
if (len == 0
return 0 ;

const char * end;
size_t out_size;

// 修剪前导空间
while (isspace(* str))
str ++;

if (* str == 0 // 所有空格?
{
* out = 0 ;
return 1 ;
}

// 修剪尾随空格
end = str + strlen(str) - 1 ;
while (end> str&& isspace(* end))
end--;
end ++;

// 将输出大小设置为修剪字符串长度和缓冲区大小减去1的最小值
out_size =(end-str)< len - 1 ? (end - str):len - 1 ;

// 复制修剪后的字符串并添加空终结符
memcpy (out,str,out_size);
out [out_size] = 0 ;

return out_size;
}

void 执行命令( const char * command)
{
// Fork and在这里执行您的流程
}





(方法TrimWhiteSpace,我在StackOverflow找到:如何以标准方式修剪前导/尾随空格? [ ^ ])



此链接可能也很有趣:执行程序:execvp()系统调用 [ ^ ]


I want to make a shell where the child process runs linux commands(with the help of execvp) such as "ls" etc.The problem is that i also want it to support pipe commands such as "ls /tmp | wc -l" .Τhe program i have for now works for commands like "ls" or "ls -l /tmp"

解决方案

For dup2 you can have a look here: dup2(2) - Linux man page[^]

For the code, start with an outer loop where you split the input string at the pipe character. Then create a function that takes care of the execution of the each command.

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>

size_t TrimWhiteSpace(char *out, size_t len, const char *str);
void ExecuteCommand(const char* command);

int main()
{	
    char input[80] = "ls / tmp | wc - l";
    const char sep[2] = "|";
    char *token = NULL;
    char* command = NULL;
    
    token = strtok(input, sep);
    
    while (token != NULL)
    {
        command = (char*)calloc(strlen(token) + 1, sizeof(char));
        TrimWhiteSpace(command, strlen(token) + 1, token);
        
        printf("%s\n", command);
        
        ExecuteCommand(command);
        
        token = strtok(NULL, sep);
    }
    return 0;
}

size_t TrimWhiteSpace(char *out, size_t len, const char *str)
{
    if (len == 0)
    	return 0;
    
    const char *end;
    size_t out_size;
    
    // Trim leading space
    while (isspace(*str)) 
    	str++;
    
    if (*str == 0)  // All spaces?
    {
    	*out = 0;
    	return 1;
    }
    
    // Trim trailing space
    end = str + strlen(str) - 1;
    while (end > str && isspace(*end)) 
    	end--;
    end++;
    
    // Set output size to minimum of trimmed string length and buffer size minus 1
    out_size = (end - str) < len - 1 ? (end - str) : len - 1;
    
    // Copy trimmed string and add null terminator
    memcpy(out, str, out_size);
    out[out_size] = 0;
    
    return out_size;
}

void ExecuteCommand(const char* command)
{
    // Fork and execute your processes here
}



(The method TrimWhiteSpace, I found at StackOverflow: How do I trim leading/trailing whitespace in a standard way?[^])

This link might be interesting as well: Execute a Program: the execvp() System Call[^]


这篇关于用管道运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆