[英]Append a text to all files under specific folder
I need to append text to the bottom of the files into .htaccess files under /home directory for all of my client's website.我需要将文本附加到文件的底部到我所有客户网站的 /home 目录下的 .htaccess 文件中。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}`
I already tried to echo 'code' >> .htaccess
and failed because it contains new line, I've also tried \\n
or \\r
without success and I'm using printf
;我已经尝试
echo 'code' >> .htaccess
并失败,因为它包含新行,我也尝试过\\n
或\\r
没有成功,我正在使用printf
; I can add new line but it says我可以添加新行,但它说
bash: printf: `}': invalid format character
Update..更新..
Im successfully fix the invalid format character by following tripleee code but unfortunately it not append all of my .htaccess files我通过遵循三元组代码成功修复了无效格式字符,但不幸的是它没有附加我所有的 .htaccess 文件
This is the code这是代码
for file in /home/*/public_html/.htaccess; do
printf '%s\n' '# Redirect to https' \
'RewriteEngine On' \
'RewriteCond %{HTTPS} off' \
'RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}' >>".htaccess"
done
Im able to resolved the issue by using these code我能够通过使用这些代码解决问题
printf '%s\n' '# Redirect to https' \
'RewriteEngine On' \
'RewriteCond %{HTTPS} off' \
'RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}' |
tee -a /home/*/public_html/.htaccess
Thanks to tripleee感谢三重奏
You need to double the %
to produce a literal per-cent character in a printf
format string.您需要将
%
加倍以在printf
格式字符串中生成文字百分比字符。 Or, pass an explicit format string to printf
and pass the individual lines as arguments.或者,将显式格式字符串传递给
printf
并将各个行作为参数传递。
printf '%s\n' 'RewriteEngine On' \
'RewriteCond %{HTTPS} off' \
'RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}' |
tee -a /home/*/public_html/.htaccess >/dev/null
There's no reason you couldn't use echo
too, though it's slightly hard on the eyes.没有理由您也不能使用
echo
,尽管它对眼睛来说有点困难。
echo 'RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}' |
tee -a /home/*/public_html/.htaccess >/dev/null
You can get something similar with echo -e
but it's less portable and less elegant than printf
.你可以用
echo -e
得到类似的东西,但它不如printf
便携和优雅。
You could also simply use a here document:您也可以简单地使用此处的文档:
tee -a /home/*/public_html/.htaccess<<-'____HERE' >/dev/null
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
____HERE
Putting a dash (minus) before the here-document separator allows you to use tabs (but not spaces!) for indentation;在此处文档分隔符之前放置一个破折号(减号)允许您使用制表符(但不能使用空格!)进行缩进; the leading tabs will be stripped from the text.
前导标签将从文本中删除。 Putting quotes around the separator causes the shell to quote the document (ie no dollar signs or backticks in the document will be evaluated).
在分隔符周围加上引号会导致 shell 引用文档(即不会评估文档中的美元符号或反引号)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.