我使用管道,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) 我需要将某些日志条目传递给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) 对于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",任何人都可以通过示例说明如何使用它.
提前感谢任何建议.
如何在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()在新行之后终止.如何将整个事物捕获为字符串/列表?
嗨,我正在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) 我写了这个函数来与外部程序进行通信.这样的程序从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) 我想在字符串下解析 -
输入: "1"|"abc xyz"||"a|25|30"|2345
输出:
"1"
"abc xyz"
"a|25|30"
2345
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
有什么区别
cat int.txt > out.txt
Run Code Online (Sandbox Code Playgroud)
和
cat int.txt >> out.txt
Run Code Online (Sandbox Code Playgroud)
有>>什么不同>吗?
在一门课程中,老师给了我们一些代码(在粉笔板上),但是他手写的很糟糕,而且我不能做出一些部分.管道和叉子也是新的,所以没有帮助.
#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) 考虑以下示例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不会工作,因为它的输出仍然在管道的边界内,呃!
非常感谢.