我正在编写一个脚本,以便更有效地设置静态站点。我遇到的问题是我用来替换文件中字符串的变量具有未转义的正斜杠并且是用户输入。我怎样才能确保_site_url: http://statamic.com
会变成_site_url: http://example.com
?
只要不存在正斜杠,下面的代码就可以工作。
echo "What's your site URL? Don't forget the protocol (ex. http://)!"
read -e SITE_URL
echo "%s/_site_url: http:\/\/statamic.com/_site_url: $SITE_URL/g
w
q
" | ex _config/settings.yaml
由于您特别提到了 bash 而不是 Bourne shell 或通用 Unix shell,我建议使用 Bash 的内置搜索/替换功能:
echo "What's your site URL? Don't forget the protocol (ex. http://)!"
read -e SITE_URL
echo Escaped URL: ${SITE_URL//\//\\\/}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)