cat file | … vs … <file

前端 未结 7 1996
北荒
北荒 2020-12-16 13:59

Is there a case of ... or context where cat file | ... behaves differently than ... ?

7条回答
  •  囚心锁ツ
    2020-12-16 14:25

    One further difference is behavior on a blocking open() of the input file.

    For example, assuming input is a FIFO with no writers, one invocation will not spawn any child programs until the input file is opened, while the other will spawn two processes:

    prog ... < a_fifo      # 'prog' not launched until shell can open file
    cat a_fifo | prog ...  # 'prog' and 'cat' are running (latter may block on open)
    

    In practice this rarely matters except in contrived circumstances. prog might periodically log or do some cleanup work while waiting for input, for example, which you might want to happen even if no input is available. (Why wouldn't prog be sophisticated enough to open its own input fifo nonblocking?)

提交回复
热议问题