如何在外壳程序中使用dup2()和pipe()来实现“ ls | wc”?

时间:2019-03-09 00:58:11

标签: c

我将如何使用dup2()和pipe()函数将ls的输出重定向到wc的输入。因此打印字数。

这是我的尝试,但是这不起作用。

<button id="nc_1" data-bt="NeoCities One" type="button">NeoCities button 1</button>
<button id="nc_redirect" data-bt="NeoCities Loader" type="button">Stop me loading!</button>

<script>
  document.querySelector('#nc_1').addEventListener('click', btFun);
  document.querySelector('#nc_redirect').addEventListener('click', redirect);

  function btFun(e) {
    alert(e.target.value || e.target.innerText);  
  }

  function redirect() {
    console.log('redirect called');
    //window.location.assign("image.html");
    window.top.location.href = "image.html";
  }
</script>

}

1 个答案:

答案 0 :(得分:0)

所以你有6个明显的错误。

  1. 缩进您的代码。
  2. 仅在子进程中调用dup2。
  3. 1个不是标准输出,0个不是标准输入。
  4. 如果execvp失败,请调用_exit。
  5. wait(NULL)在第二个fork之前,而不是
  6. dup2的参数被交换

因为这是家庭作业,所以我不会只给你答案。将此形状定型并ping通,我会发现还能找到更多的东西。