【问题标题】:while read loop with input from process substitution not exiting on break in zshwhile read loop with input from process substitution not exiting on break in zsh
【发布时间】:2022-12-01 15:38:24
【问题描述】:

Using zsh, I was trying to break the while loop after file move event, but break happens only after the second one. This only occurs when I try to execute script

#!/bin/zsh

while read changed; do
  echo $changed
  if [ $changed = MOVE_SELF ]; then
    echo "File was moved."
    break
  fi
done < <(inotifywait -m -e move_self --format "%e" $1)

echo "HI THERE"

in zsh. However, if I try the same code with #!/bin/bash it works as expected (loop breaks after the first event).

【问题讨论】:

  • I'd suggest tagging this only zsh and not bash; the bash behavior is well-understood, so you need only zsh experts looking at this question, not bash experts.
  • @rowboat, Thank you. &lt;(... &amp;) works fine. Would you mind writing an answer? I need -m, example above is simplified.
  • 请问为什么进程替换有两个&lt;标志,即done &lt; &lt;(inotify ...)?我本以为只有一个。
  • @user1934428,看看这个:stackoverflow.com/a/28927847/15036204

标签: zsh process-substitution


【解决方案1】:

Behavior in zsh and bash is different. One of the possible solutions is to use &amp; for command in process substitution. Diff:

< done < <(inotifywait -m -e move_self --format "%e" $1)
---
> done < <(inotifywait -m -e move_self --format "%e" $1 &)

Looks like zsh is blocking waits for process substitution to execute again. Output of zsh -x after first execution of inotitywait without &amp;:

+script.sh:3> read -r changed
+script.sh:3> inotifywait -m -e move_self --format %e file.lua
Setting up watches.
Watches established.
+script.sh:4> echo MOVE_SELF
MOVE_SELF
+script.sh:5> [ MOVE_SELF '=' MOVE_SELF ']'
+script.sh:6> echo 'File was moved.'
File was moved.
+script.sh:7> break

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-27
    • 2022-12-02
    • 2022-08-12
    • 2017-03-03
    • 1970-01-01
    相关资源
    最近更新 更多