While loop to test if a file exists in bash

前端 未结 8 557
旧巷少年郎
旧巷少年郎 2020-12-23 10:37

I\'m working on a shell script that does certain changes on a txt file only if it does exist, however this test loop doesn\'t work, I wonder why? Thank you!



        
8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 11:25

    If you are on linux and have inotify-tools installed, you can do this:

    file=/tmp/list.txt
    while [ ! -f "$file" ]
    do
        inotifywait -qqt 2 -e create -e moved_to "$(dirname $file)"
    done
    

    This reduces the delay introduced by sleep while still polling every "x" seconds. You can add more events if you anticipate that they are needed.

提交回复
热议问题