【问题标题】:inotifywait To run command based on type of file modifiedinotifywait 根据修改的文件类型运行命令
【发布时间】:2012-09-04 14:36:41
【问题描述】:

我特别想在 2 个子目录中同时查看 2 个不同的文件类型:

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

我只知道这么多 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



// 编辑 // 修改了这一行:

while inotifywait --format %w%f ./{css,js}/*.{styl,coffee}; do

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

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

Setting up watches.
Watches established.
./css/styles.styl

【问题讨论】:

标签: regex bash inotify watch inotify-tools


【解决方案1】:

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

#!/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

如果您有更好的解决方案,只查看我想要的文件,那么我会全力以赴

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多