我在Perl .pl文件中有以下代码。您是否认为此代码有任何问题(我不明白它的工作方式,因为第二行中有一个“ |”字符,而后面没有命令)
while ( $temp ne "" ) {
open( PS, "ps -ef | grep deploy.sh | grep ssh | grep -v grep|" );
$temp = <PS>;
close(PS);
print "The Deploy scripts are still running. Now sleeping 20\n";
sleep 20;
}
Run Code Online (Sandbox Code Playgroud) 我有一个Go流程,通过管道接受输入.
tail -f something.foo | 去运行myprog.go
由于stdin是管道的输出,我找不到在myprog.go中读取键盘输入的方法.这可能吗?
我考虑在myprog.go中执行tail命令,但是我想避免在myprog.go崩溃并且无法终止另一个进程时创建另一个进程.
#include <stdio.h>
int main()
{
FILE* cmd = popen("grep Hello", "w");
fwrite("Hello\n", 6, 6, cmd);
fwrite("Hillo\n", 6, 6, cmd);
fwrite("Hello\n", 6, 6, cmd);
pclose(cmd);
}
Run Code Online (Sandbox Code Playgroud)
上述程序输出:
二进制文件(标准输入)匹配
为什么grep会给出消息,以及如何修复它?
为什么如下
$ echo "test\|String" | sed 's/(?!\||\\)./&./g'
test\|String
Run Code Online (Sandbox Code Playgroud)
不生产:
t.e.s.t.\|S.t.r.i.n.g.
Run Code Online (Sandbox Code Playgroud)
我测试了正则表达式代码,它在我的测试中正确选取字符串
我希望它能做到以下几点:
$ echo "test\|String" | sed 's/(?!\||\\)./&./g'
t.e.s.t.\|S.t.r.i.n.g.
Run Code Online (Sandbox Code Playgroud) 我有一个脚本,其中包含这样一行:
curl example.com | grep "some words | xargs mkdir | ad --nauseum
我的问题是,我是否可以在重定向输出时将其分成多行(或者这种情况是否有更好的模式?)
管道的正式定义表明左侧文件的STDOUT将立即通过管道传送到正确文件的STDIN.我有两个文件,hello.txt和human.txt. cat hello.txt返回Hello并cat human.txt返回I am human.如果我这样做cat hello.txt | cat human.txt,不应该返回Hello I am human吗?相反,我正在看command not found.我是shell脚本的新手.有人可以解释一下吗?
以下工作并打印命令输出:
out, err := exec.Command("ps", "cax").Output()
Run Code Online (Sandbox Code Playgroud)
但是这个失败了(退出状态为1):
out, err := exec.Command("ps", "cax | grep myapp").Output()
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我们有一个脚本,带有返回码.例如
#!/bin/bash
exit 42
Run Code Online (Sandbox Code Playgroud)
哪个工作正常:
$ ./script ; echo $?
42
Run Code Online (Sandbox Code Playgroud)
但如果我去:
$ bash << EOF
./script ; echo $?
EOF
0
Run Code Online (Sandbox Code Playgroud)
所以它打印0,而人们会期望它仍然打印42
我正在编写一个应用程序,它首先从unix管道接收数据,然后提示用户输入.我似乎无法弄清楚为什么在提示用户输入之前管道的输入似乎没有正确关闭.感觉我在这里缺少一些非常基本的东西.
我曾尝试提出了冲洗标准输入所有的例子在这里没有成功.这也似乎有可能相关,但我还没有设法提取任何相关的答案.
#include <stdio.h>
#include <stdlib.h>
#define BUFSZ 1024
int main(void) {
char *buf = calloc(BUFSZ, sizeof(char));
while(fgets(buf, BUFSZ, stdin)) { printf("Pipe input: %s", buf); }
printf("Enter input: ");
fgets(buf, BUFSZ, stdin);
printf("User input: %s", buf);
free(buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用法和输出示例:
$ cat testdata2 | ./a.out
Pipe input: testdata testdata
Pipe input: testdata testdata2
Pipe input: testdata testdata3
Pipe input: testdata testdata4
Pipe input: testdata testdata5
Pipe input: testdata testdata6
Pipe input: testdata testdata7
Enter input: …Run Code Online (Sandbox Code Playgroud) 我想创建一个可以将年份(1992年)改为年龄(26岁)(韩国时代)的管道
我想我应该加载当前年份(2017年),减去1992年并加上+1
但我不知道如何用管道来实现这一点.
我提前感谢您的帮助.