Bash 监视HTML模板并使用手柄编译的Shell脚本
我正在尝试创建一个脚本,该脚本监视目录中的HTML模板文件,并在通知更改时编译模板。我不能让它工作,这就是我得到的: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
#!/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")