bash脚本,监视文件夹,执行命令 [英] Bash script, watch folder, execute command

查看:238
本文介绍了bash脚本,监视文件夹,执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个bash脚本,它需要两个参数:一个目录的命令。我需要此目录的变化看,当事情已经改变,我需要执行的命令。我真的很新的bash脚本和真的不知道我在做什么,这样下去容易对我。我也是在Mac上,而不是Linux。任何指针,或外部资源将极大地帮助。我知道有很多人尝试这种在互联网上,没有人似乎可以得到它的权利。我真的试图模仿SASS的手表功能。

 #!/斌/庆典#。/ watch.sh $ PATH $ COMMANDDIR = $ 1ls -l命令$ DIR> $ DIR / .begin
#此不起作用
差值= $(差异.begin .END)如果[$差值='\\ n'];然后
    #files相同
其他
    $ 2
科幻ls -l命令$ DIR> $ DIR / .END


解决方案

下面是看更改一个文件夹,当一个更新运行不太编译器的一个例子。作为preREQ需要 NPM 键,这些模块的的onchange 。节点社区有不同的手表命令的一大堆(如的onchange)我不知道任何编译自包含的二进制文件。

  NPM我少的onchange -g`

然后就可以使用这样的:

 的onchange./stylesheets/*.less -  lessc main.less>的main.css

我preFER在<一个bash命令href=\"http://stackoverflow.com/questions/6475252/bash-script-watch-folder-execute-command/22495633#22495633\">Grunt回答我给了一会儿回来。

I am trying to create a bash script that takes 2 parameters: a directory and a command. I need to watch this directory for changes and when something has been changed I need to execute the command. I'm really new to bash scripting and am not really sure what I am doing, so go easy on me. I'm also on a mac, not linux. Any pointers, or external resources would greatly help. I know that a lot of people try this on the internet and no one can seem to get it right. I am really trying to mimic SASS's watch functionality.

#!/bin/bash

#./watch.sh $PATH $COMMAND

DIR=$1  

ls -l $DIR > $DIR/.begin
#this does not work
DIFFERENCE=$(diff .begin .end)

if [ $DIFFERENCE = '\n']; then
    #files are same
else
    $2
fi 

ls -l $DIR > $DIR/.end

解决方案

Here's an example of watching a folder for changes and running a the less compiler when one is updated. As a prereq you need npm and these the module onchange. The node community has a whole bunch of different watch commands (like onchange) I'm not aware of any that are compiled self-contained binaries.

npm i less onchange -g` 

Then you can use something like:

onchange "./stylesheets/*.less" -- lessc main.less > main.css

I prefer a BASH command over the Grunt answer I gave a while back.

这篇关于bash脚本,监视文件夹,执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆