inotifywait根据修改的文件类型运行命令

Mar*_*nas 5 regex bash watch inotify inotify-tools

我特意试图在2个子目录中同时观察2种不同的文件类型:


  'css'子目录中  'js'子目录.styl文件类型中的.coffee文件类型

我只知道这么多bash而且我正试图让这样的东西发挥作用

while inotifywait --format %w%f ./{css,js}/*.{styl,coffee}; do
  # regex pattern to match the file ending
  # and define it to $ending

  if[ $ending == coffee ]
  then
   coffee -c $file
  elif[ $ending == styl ]
  then
    stylus -c $file
  fi
done
Run Code Online (Sandbox Code Playgroud)



//编辑//修改此行:

while inotifywait --format %w%f ./{css,js}/*.{styl,coffee}; do
Run Code Online (Sandbox Code Playgroud)

所以现在它检查两个文件,但是如果css文件夹上没有.coffee文件,或者js中没有.styl文件,则会返回错误,表明没有找到文件.

另外我注意到当我运行这个脚本时,它返回/运行以下3次

Setting up watches.
Watches established.
./css/styles.styl
Run Code Online (Sandbox Code Playgroud)

Mar*_*nas 2

不是最干净的解决方案,但有效

#!/bin/sh

while file=$(inotifywait -r -e modify --format "%w%f" ./); do
  EXT=${file##*.}
  if [ $EXT = "styl" ]
  then
    stylus -c $file
  fi
  if [ $EXT = "coffee" ]
  then
    coffee -c $file
  fi
done
Run Code Online (Sandbox Code Playgroud)

如果您有更好的解决方案,只观看我想要的文件,那么我会洗耳恭听