重击字符串操作不需要删除换行符 [英] Bash string operations unwanted remove new line characters

查看:59
本文介绍了重击字符串操作不需要删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是bash脚本的新手,所以请问....我正在尝试将一些html内容与包含标准标题的模板结合在一起,该模板具有一个占位符"REPLACEME",我认为我可以找到并替换它.该循环仅对目录中的所有文件重复该操作.

I'm completely new to bash scripting so excuse me.... I am trying to combine some html content with a template that contains standard headings, the template has a place-holder "REPLACEME" which I thought I could just find and replace on. The loop simply repeats the operation on all the files in the directory.

REPLACEME="REPLACEME"
for file in *.html
do
TEMPLATE=$(<../template/template.html)
CONTENT=$(<$file)
OUTPUT="${TEMPLATE/"$REPLACEME"/"$CONTENT"}"
echo $OUTPUT > ../compiled/$file
done

这可以工作,但是生成的html文件中已经删除了换行符,这使其看起来像垃圾!有人可以帮忙吗?

This works but the resulting html file has been stripped of new line characters, which makes it look like junk! Can anyone help?

推荐答案

替换:

echo $OUTPUT > ../compiled/$file

使用:

echo "$OUTPUT" > ../compiled/$file

shell对未加引号的变量执行分词.使用 IFS 的默认值,这意味着所有空格序列(包括制表符和换行符)都将替换为一个空格.为防止这种情况,请将该变量放在双引号中,如上所示.

The shell performs word splitting on unquoted variables. With the default value for IFS, this means that all sequences of whitespace, which includes tabs and newlines, are replaced with a single blank. To prevent that, put the variable in double-quotes as shown above.

这篇关于重击字符串操作不需要删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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