2012-09-25 42 views
1

我正在使用handlebars.js为我们的单页应用程序呈现客户端模板(在node.js服务器端使用Jade)。如何在bash命令行上输出文件列表

我想在服务器端预编译句柄模板并将它们捆绑为一个JS文件发送给客户端。

目前,我用handlebars编译模板,像这样:

$ handlebars template1.handlebars template2.handlebars -f precompiled_templates.js 

我想编写一个bash脚本,可以读取所有的目录中*.handlebars文件,然后通过把手编译运行它们。所以,如果我有一个目录,如下所示:在模板目录

templates/ 
    temp1.handlebars 
    temp2.handlebars 
    temp3.handlebars 
    temp4.handlebars 

运行我的bash脚本(或者一个行命令)将基本上运行以下handlebars命令:

$ handlebars temp1.handlebars temp2.handlebars temp3.handlebars temp4.handlebars -f precompiled_templates.js 

有谁知道我如何编写一个bash脚本来获取目录中的所有句柄文件到上面的命令行中?

回答

2

它看起来像你想类似

handlebars templates/*.handlebars -f precompiled_templates.js 

的东西,除非你将与“模板”前缀,以每个文件结束了。我的首选方法需要两行:

files=(templates/*.handlebars) 
handlebars "${files[@]#templates/}" -f precompiled_templates.js. 

第一行将所有需要的文件放在一个数组中。在第二行中,我们展开数组的内容,但从结果扩展中的每个元素中剥离“templates /”前缀。

+0

我试过'$ handlebars * .handlebars -f ...'但它没有工作,但那是因为我指向错误的目录!问题解决了! – Gaurav

0

在bash中,列出了可与双引号中的字符串创建:

FILES="/etc/hosts /etc/passwd" 
for file in $FILES; do cat $file ; done 

<cat all files> 

您还可以使用find和exec命令。

人[找到| EXEC]欲了解更多信息

1

在模板目录下执行:

handlebars `find -name *.handlebars` -f precompiled_templates.js 

背蜱意味着它会执行该命令,然后返回结果,所以你实际上你对每个文件运行它返回车把。如上所述,find会查找当前文件和子目录中的文件,因此请确保从正确的位置运行它。

相关问题