Bash 替换切断字符串的结尾
Bash substitution cut off end of string
我有 bash 脚本,它应该替换用户输入的文本模板中的一些占位符。
#!/bin/bash
# Run this script as bash command like: bash create-apache-site.sh
read -p 'Write url without www and http prefixes: ' url
template=$(</etc/apache2/sites-available/000-default.conf)
template2=("${template/1*****/$url}")
echo "$template2" > /home/Camo/template.txt
模板文件是带有占位符(1*****、2*****、...)的多行字符串,看起来像
<VirtualHost *:80>
ServerAdmin xxxx.xxxxx@gmail.com
ServerName 1*****
ErrorLog ${APACHE_LOG_DIR}/www/2*****/error.log
CustomLog ${APACHE_LOG_DIR}/www/3*****/access.log combined
DocumentRoot /var/www/html/4*****
<Directory /var/www/html/5*****/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} = 6*****
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
但是这个脚本的结果是这个损坏的文件
<VirtualHost *:80>
ServerAdmin xxxx.xxxxx@gmail.com
ServerName ooooooooooooooo.com
如您所见,替换切断了字符串的末尾。有人可以告诉我它有什么问题吗?非常感谢。
您必须转义 *
才能使替换生效。
也许更好的方法是不使用 *
作为占位符?但我把它留给你。
试试这个:
(我还进行了全局替换,以便您可以在文件中多次重复使用相同的占位符)
#!/bin/bash
# Run this script as bash command like: bash create-apache-site.sh
read -p 'Write url without www and http prefixes: ' url
template=$(</etc/apache2/sites-available/000-default.conf)
template2=("${template//1\*\*\*\*\*/$url}")
echo "$template2" > /home/Camo/template.txt
我有 bash 脚本,它应该替换用户输入的文本模板中的一些占位符。
#!/bin/bash
# Run this script as bash command like: bash create-apache-site.sh
read -p 'Write url without www and http prefixes: ' url
template=$(</etc/apache2/sites-available/000-default.conf)
template2=("${template/1*****/$url}")
echo "$template2" > /home/Camo/template.txt
模板文件是带有占位符(1*****、2*****、...)的多行字符串,看起来像
<VirtualHost *:80>
ServerAdmin xxxx.xxxxx@gmail.com
ServerName 1*****
ErrorLog ${APACHE_LOG_DIR}/www/2*****/error.log
CustomLog ${APACHE_LOG_DIR}/www/3*****/access.log combined
DocumentRoot /var/www/html/4*****
<Directory /var/www/html/5*****/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} = 6*****
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
但是这个脚本的结果是这个损坏的文件
<VirtualHost *:80>
ServerAdmin xxxx.xxxxx@gmail.com
ServerName ooooooooooooooo.com
如您所见,替换切断了字符串的末尾。有人可以告诉我它有什么问题吗?非常感谢。
您必须转义 *
才能使替换生效。
也许更好的方法是不使用 *
作为占位符?但我把它留给你。
试试这个:
(我还进行了全局替换,以便您可以在文件中多次重复使用相同的占位符)
#!/bin/bash
# Run this script as bash command like: bash create-apache-site.sh
read -p 'Write url without www and http prefixes: ' url
template=$(</etc/apache2/sites-available/000-default.conf)
template2=("${template//1\*\*\*\*\*/$url}")
echo "$template2" > /home/Camo/template.txt