标签: pipe

有管道,叉子,dup2的问题

我使用管道,fork,dup2来实现"ls | 更多"或"ls | 排序"等我在这里无法理解这个问题.当我运行我的程序时,我收到此错误:

./a.out  
Missing filename ("less --help" for help)
Run Code Online (Sandbox Code Playgroud)

为什么我会"少"?

这段代码有什么问题?如果我再次将"更多"更改为"ls",则可以正常工作.我的意思是,它就像做ls | LS.

#define STDIN 0
#define STDOUT 1

int main()
{
   int fd[2];
   int pid;
   char *lschar[20]={"ls",NULL};
   char *morechar[20]={"more",NULL};
   pid = fork();
   if (pid == 0) {
   /* child */
     int cpid;
     cpid = fork();
     if(cpid == 0) {
       //printf("\n in ls \n");
       pipe(fd);
       dup2(fd[1], STDOUT);
       close(fd[0]);
       close (fd[1]);
       execvp("ls",lschar);
     } else if(cpid>0) {
       waitpid(cpid, NULL,0);
       dup2(fd[0],STDIN);
       close(fd[0]);
       close(fd[1]);
       execvp("more", morechar);
     }
   } else if (pid …
Run Code Online (Sandbox Code Playgroud)

c linux fork pipe

1
推荐指数
1
解决办法
3348
查看次数

管道进入perl脚本

我需要将某些日志条目传递给perl脚本,但是我无法使用ARGV或STDIN来使用它.

tail -f messages | grep --line-buffered "auth failure:" | awk '{print $1,$2,$3,$10}' | test3.pl
Run Code Online (Sandbox Code Playgroud)

也许某些东西正在被缓冲,但似乎没有什么东西可以用于test3.pl,但是如果我放弃了| test3.pl那么我会看到应该进入perl:

Feb 3 16:09:36 [user=someusername]  
Run Code Online (Sandbox Code Playgroud)

streaming perl real-time pipe

1
推荐指数
1
解决办法
4577
查看次数

如何在Bash脚本中将Bash命令输出传递给多行Perl代码?

对于Bash脚本中的以下管道:

Bash command | perl -ne 'single line perl command' | Another Bash command
Run Code Online (Sandbox Code Playgroud)

Perl命令只能是单行.如果我想编写更复杂的多行Perl命令怎么样?我可以为每个perl命令行使用多个"-e"选项,例如:

perl -n -e 'command line 1' -e 'command line 2' -e 'command line 3'
Run Code Online (Sandbox Code Playgroud)

或者我可以为多行Perl代码使用"Here Document"(在这种情况下仍然可以指定perl选项,例如"-n").

如果可以使用"Here Document",任何人都可以通过示例说明如何使用它.

提前感谢任何建议.

bash perl pipe

1
推荐指数
1
解决办法
1938
查看次数

管道的Python raw_input失败

如何在python中捕获管道文本.例如像这样的东西

cat foo.py | ./foo.py
Run Code Online (Sandbox Code Playgroud)

在foo.py我有以下内容:

if __name__ == "__main__":
  text = raw_input()
Run Code Online (Sandbox Code Playgroud)

问题是raw_input()在新行之后终止.如何将整个事物捕获为字符串/列表?

python pipe

1
推荐指数
1
解决办法
684
查看次数

Qt GUI应用程序意外结束

嗨,我正在Linux上工作,我正在尝试创建一个GUI应用程序,以配合我已经制作的可执行文件.

出于某种原因,它意外地结束了.没有错误消息,它只是在Qt控制台窗口中说它意外地以退出代码0结束.

有人可以帮我看一下.我在Linux上工作.

我也会在这里粘贴代码.

void MainWindow::on_pushButton_clicked()
{
    QString stringURL = ui->lineEdit->text();

    ui->labelError->clear();
    if(stringURL.isEmpty() || stringURL.isNull()) {
        ui->labelError->setText("You have not entered a URL.");
        stringURL.clear();
        return;
    }

    std::string cppString = stringURL.toStdString();
    const char* cString = cppString.c_str();

    char* output;

    //These arrays will hold the file id of each end of two pipes
    int fidOut[2];
    int fidIn[2];

    //Create two uni-directional pipes
    int p1 = pipe(fidOut);                //populates the array fidOut with read/write fid
    int p2 = pipe(fidIn);                 //populates the array fidIn  with read/write fid
    if …
Run Code Online (Sandbox Code Playgroud)

c c++ qt pipe

1
推荐指数
1
解决办法
537
查看次数

C pipe()在一定数量的调用后返回错误

我写了这个函数来与外部程序进行通信.这样的程序从stdin获取输入并在stdout上输出它的输出.为了使我的代码与该程序通信,我使用管道将stdin和stdout重定向到缓冲区.

int query_oracle(mpz * c,int *t, mpz * m) {
  int out_pipe[2];
  int in_pipe[2];
  int saved_stdout;
  int saved_stdin;

  // REDIRECT STDIN
  saved_stdin = dup(STDIN_FILENO);      /* save stdin for later */
  pipe(in_pipe);            /* make a pipe */
  close(STDIN_FILENO);
  dup2(in_pipe[0], STDIN_FILENO);   /* redirect pipe to stdin */
  //write(in_pipe[1], in_buf, strlen(in_buf));

  // REDIRECT STDOUT
  saved_stdout = dup(STDOUT_FILENO);  /* save stdout for display later */
  if( pipe(out_pipe) != 0 ) {          /* make a pipe */
  exit(1);
  }
  dup2(out_pipe[1], STDOUT_FILENO);   /* redirect stdout to …
Run Code Online (Sandbox Code Playgroud)

c linux stdin stdout pipe

1
推荐指数
1
解决办法
369
查看次数

管道和引号的Perl或python正则表达式拆分

我想在字符串下解析 -

输入: "1"|"abc xyz"||"a|25|30"|2345

输出:

"1"
"abc xyz"

"a|25|30"
2345
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

regex perl split pipe

1
推荐指数
1
解决办法
305
查看次数

1
推荐指数
1
解决办法
77
查看次数

管/叉有问题.无法理解手册的一半(男)

在一门课程中,老师给了我们一些代码(在粉笔板上),但是他手写的很糟糕,而且我不能做出一些部分.管道和叉子也是新的,所以没有帮助.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
void main () {
    int a[] = {1,2,3,4}, f[2];  // ok so we initialize an array and a "pipe folder" (pipefd in manual) ?
    pipe (f);                   // not 100% sure what this does ?
    if (fork () == 0) {         // if child
        close (f[0]);               // close pipe read-end
        a[0] += a[1];               
        write (f[1], &a[0], sizeof (int))      // fixed
        close (f[1]);               // close pipe write-end
        exit(0);                    // closes child and sends …
Run Code Online (Sandbox Code Playgroud)

c fork pipe

1
推荐指数
1
解决办法
558
查看次数

如何在没有循环的连续变换管道中显示每个步骤的结果?

考虑以下示例Bash one-liner,其中字母"h","e"和"o"一次一个地从单词"hello"中删除,按顺序排列.只留下两个"l"字母;

$ echo "hello" | tr -d h | tr -d e | tr -d o
ll
Run Code Online (Sandbox Code Playgroud)

我试图找到一种方法,用于在一个班轮内向屏幕显示每个命令的输出,因此运行它的其他人可以看到发生了什么.继续上面的例子,我想输出如下;

$ echo "hello" | tr -d h | tr -d e | tr -d o
hello
ello
llo
ll
Run Code Online (Sandbox Code Playgroud)

这可能吗?根据上面单行的操作,我们将命令的输出带到垂直管道的命令.所以我假设我必须从管道中断打印到stdout,这会中断我写的"命令链".或者也许tee可以在这里使用,但我似乎无法实现欲望的影响. 更新:tee不会工作,因为它的输出仍然在管道的边界内,呃!

非常感谢.

bash pipe

1
推荐指数
1
解决办法
151
查看次数

标签 统计

pipe ×10

c ×4

perl ×3

bash ×2

fork ×2

linux ×2

c++ ×1

command ×1

python ×1

qt ×1

real-time ×1

regex ×1

split ×1

stdin ×1

stdout ×1

streaming ×1

unix ×1