Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 监视HTML模板并使用手柄编译的Shell脚本_Bash_Shell_Templates_Compilation_Handlebars.js - Fatal编程技术网

Bash 监视HTML模板并使用手柄编译的Shell脚本

Bash 监视HTML模板并使用手柄编译的Shell脚本,bash,shell,templates,compilation,handlebars.js,Bash,Shell,Templates,Compilation,Handlebars.js,我正在尝试创建一个脚本,该脚本监视目录中的HTML模板文件,并在通知更改时编译模板。我不能让它工作,这就是我得到的: #!/bin/sh while FILENAME=$(inotifywait --format %w -r templates/*.html) do COMPILED_FILE=$(echo "$FILENAME" | sed s/templates/templates\\/compiled/g | sed s/.html/.js/g) handlebars $FILEN

我正在尝试创建一个脚本,该脚本监视目录中的HTML模板文件,并在通知更改时编译模板。我不能让它工作,这就是我得到的:

#!/bin/sh
while FILENAME=$(inotifywait --format %w -r templates/*.html)
do
  COMPILED_FILE=$(echo "$FILENAME" | sed s/templates/templates\\/compiled/g | sed s/.html/.js/g)
  handlebars $FILENAME -f $COMPILED_FILE -a
done
我使用它来查看当前目录,尽管我希望它也检查子目录。然后,需要将编译后的文件保存在名为
templates/compiled
的子目录中,可以选择使用该子目录

因此,
templates/foo.html
需要编译并存储为
templates/compiled/foo.js

因此
templates/other/foo.html
需要编译并存储为
templates/compiled/other/foo.js

正如你所看到的,我试图观看导演,并用
templates/compiled
替换
templates/
名称

欢迎任何帮助

一些观察,然后是一个解决方案:

传递参数
-r templates/*.html
仅匹配
模板/
中的.html文件,而不匹配
模板/other/
中的.html文件。相反,我们要做的是
-r templates
,它会通知我们对
templates
下的任何文件的更改

如果不在
--monitor
模式下使用
inotifywait
,您将丢失在
把手
运行的短暂时间内更改的任何文件(如果一次保存所有打开的文件,可能会发生这种情况)。最好这样做:

#!/bin/bash
watched_dir="templates"
while read -r dirname events filename; do
    printf 'File modified: %s\n' "$dirname$filename"
done < <(inotifywait --monitor --event CLOSE_WRITE --recursive "$watched_dir")
请注意,我们可以利用-无需使用
sed

综上所述,我们得到:

#!/bin/bash
watched_dir="templates"
while read -r dirname events filename; do
    [[ "${filename##*.}" != 'html' ]] && continue

    output_path="$watched_dir/compiled/${dirname#*/}${filename%.html}.js"
    handlebars "$dirname$filename" -f "$output_path" -a
done < <(inotifywait --monitor --event CLOSE_WRITE --recursive "$watched_dir")
#/bin/bash
监视的\u dir=“模板”
读取时-r dirname事件文件名;做
[[“${filename##*.}”!='html']&继续(&C)
output_path=“$wasted_dir/compiled/${dirname#*/}${filename%.html}.js”
把手“$dirname$filename”-f“$output\u path”-a

谢谢你的解释,你的天才!
#!/bin/bash
watched_dir="templates"
while read -r dirname events filename; do
    [[ "${filename##*.}" != 'html' ]] && continue

    output_path="$watched_dir/compiled/${dirname#*/}${filename%.html}.js"
    handlebars "$dirname$filename" -f "$output_path" -a
done < <(inotifywait --monitor --event CLOSE_WRITE --recursive "$watched_dir")