I am trying to understand the internal working of pipes and C when I noticed if I run
当我注意到我跑的时候,我正试图理解管道和C的内部工作
int main() {
system("ls | grep d | wc");
}
Output:
输出:
3 3 53
But on running the same command with bash I get
但是在用bash运行相同的命令时我得到了
3 3 104
Output of ls | grep d
输出ls | grep d
question_1.pdf question_2.pdf question_2_dataset.txt
question_1.pdf question_2.pdf question_2_dataset.txt
Can someone explain the cause of this discrepancy? The same thing occurs if I use pipe via pipe()
call in C.
有人可以解释这种差异的原因吗?如果我在C中使用管道通过管道()调用,会发生同样的事情。
3 个解决方案
#1
1
Actually I figured out problem wasn't with ls but "grep --color=always d" which is alias of grep in my bash. The colored characters have extra length which increase the length of output.
实际上我发现问题不是ls而是“grep --color = always d”,这是我的bash中的grep的别名。彩色字符有额外的长度,增加了输出的长度。
#2
0
Check out what your 'ls' command at the bash is! Try:
看看你在bash上的'ls'命令是什么!尝试:
type ls
类型ls
You probably will find that ls
is an alias of some kind.
您可能会发现ls是某种别名。
Check your bash-test again with
再次检查你的bash-test
/bin/ls | grep d | wc
/ bin / ls | grep d |厕所
If you then get th esame result as in your C-Code you will know what went wrong.
如果您在C代码中获得了esame结果,您就会知道出了什么问题。
#3
0
ls is often an alias in an interactive shell. For example, in my bash session if I do type ls
I get
ls通常是交互式shell中的别名。例如,在我的bash会话中,如果我输入ls,我得到
ls is aliased to `ls -t --group-directories-first -I .pyc -I __pycache__ -I .git --color=auto -xF'
(The alias usually comes from $HOME/.bashrc
or /etc/bash.bashrc
).
(别名通常来自$ HOME / .bashrc或/etc/bash.bashrc)。
Now if you do:
现在,如果你这样做:
sh -c 'ls | grep d | wc'
(or command ls| command grep d | command wc
) you should get absolutely the same result as with compiling
(或命令ls |命令grep d |命令wc)你应该得到与编译完全相同的结果
int main() { system("ls | grep d | wc"); }
and running it in the same directory.
并在同一目录中运行它。
#1
1
Actually I figured out problem wasn't with ls but "grep --color=always d" which is alias of grep in my bash. The colored characters have extra length which increase the length of output.
实际上我发现问题不是ls而是“grep --color = always d”,这是我的bash中的grep的别名。彩色字符有额外的长度,增加了输出的长度。
#2
0
Check out what your 'ls' command at the bash is! Try:
看看你在bash上的'ls'命令是什么!尝试:
type ls
类型ls
You probably will find that ls
is an alias of some kind.
您可能会发现ls是某种别名。
Check your bash-test again with
再次检查你的bash-test
/bin/ls | grep d | wc
/ bin / ls | grep d |厕所
If you then get th esame result as in your C-Code you will know what went wrong.
如果您在C代码中获得了esame结果,您就会知道出了什么问题。
#3
0
ls is often an alias in an interactive shell. For example, in my bash session if I do type ls
I get
ls通常是交互式shell中的别名。例如,在我的bash会话中,如果我输入ls,我得到
ls is aliased to `ls -t --group-directories-first -I .pyc -I __pycache__ -I .git --color=auto -xF'
(The alias usually comes from $HOME/.bashrc
or /etc/bash.bashrc
).
(别名通常来自$ HOME / .bashrc或/etc/bash.bashrc)。
Now if you do:
现在,如果你这样做:
sh -c 'ls | grep d | wc'
(or command ls| command grep d | command wc
) you should get absolutely the same result as with compiling
(或命令ls |命令grep d |命令wc)你应该得到与编译完全相同的结果
int main() { system("ls | grep d | wc"); }
and running it in the same directory.
并在同一目录中运行它。