bash - Golang exec.Command 多个管道

标签 bash go pipe ctags

我正在尝试用 Go 做多个管道:

ctags := exec.Command("ctags", "-x", "--c-types=f", "./tmp/"+fileName)
grep  := exec.Command("grep", "member")
awk   := exec.Command("awk", "'{$1=$2=$3=$4=\"\"; print $0}'")
grep.Stdin, _ = ctags.StdoutPipe()
awk.Stdin, _ = grep.StdoutPipe()
awk.Stdout = os.Stdout
_ = grep.Start()
_ = awk.Start()
_ = ctags.Run()
_ = grep.Wait()
_ = awk.Wait()

fmt.Println(awk)

这是预期的输出应该是这样的:

$ ctags -x --c-types=f ./tmp/genetic_algorithm.py | grep member | awk '{$1=$2=$3=$4=""; print $0}'
    def __init__(self, vector, fitness_function):
    def __init__(self, population_size=10, crossover_points=[.3, .6],
    def apply_crossover(self, genotype_1, genotype_2):
    def apply_mutation(self, child_genotype):
    def calc_population_fitness(self, individuals):
    def evolve(self):
    def final_genotype(self):
    def fitness(self):
    def generate_offspring(self, selected):
    def genotype(self, indiv):
    def get_accuracy(nn_hidden_structure=[10]):
    def parse_bitstring(self, genotype):
    def run(self):
    def select_for_crossover(self):
    def slice_vector(self, vector):
    def validate_vector(self, vector, max_nodes=100):
    def vector_from_bitstring(self, genotype):

这是我得到的:

&{/usr/bin/awk [awk '{$1=$2=$3=$4=""; print $0}'] []  0xc4204ce020 0xc42007e008 <nil> [] <nil> 0xc4201fa900 exit status 2 <nil> <nil> true [0xc4204ce020 0xc42007e008 0xc4204ce048] [0xc4204ce048] [] [] 0xc420070360 <nil>}

我发现了几个使用 Go 传递命令的示例,但其中大多数只传递两个命令。有这个例子 https://gist.github.com/dagoof/1477401 ,但是当我根据那个逻辑安排我的代码时,我遇到了同样的问题。我管道正确吗?该命令只是应该在该 Python 文件上执行 exuberant ctags,然后 grep 指示方法的任何行,然后仅忽略 grep 输出的前四列。

最佳答案

问题在于:

awk   := exec.Command("awk", "'{$1=$2=$3=$4=\"\"; print $0}'")

我只需要删除单引号。

awk   := exec.Command("awk", "{$1=$2=$3=$4=\"\"; print $0}")

关于bash - Golang exec.Command 多个管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41400960/

相关文章:

go - 为什么在将结构值设置为映射中的值时会出现 "cannot assign"错误?

c++ - Gnuplot,来自 Windows 的 C++。命令窗口打开和关闭

linux - 修改了管道写入的文件

c - 当父进程发生某些事情时,父进程如何告诉子进程做某事?

linux - 为什么这个脚本不能与 nohup 一起使用但没有?

linux - 更改密码时的源文件

c - 带有循环的 Makefile

bash - 重命名大量文件的扩展 Bash

mongodb - mongo-go-driver获取插入的文档

go - Go编译器产生奇怪的加载到x0中