使用Bash和SED我试图用URL替换js文件中的两个字符串
当我运行.sh脚本时,应该插入的两个url是输入参数
./deploy.sh https://hostname.com/a/index.html https://hostname2.com/test
但是,要使它在sed命令中可用,我必须用以下命令来转义所有正斜杠:\\?在
./deploy.sh https:\\/\\/hostname.com\\/a\\/index.html https:\\/\\/hostname2.com\\/test
如果他们逃脱,SED指挥部就在Mac OSX Sierra工作
APP_URL=$1
API_URL=$2
sed "s/tempAppUrl/$APP_URL/g;s/tempApiUrl/$API_URL/g" index.src.js > index.js
现在我不想插入转义的url作为参数,我想让脚本自己转义正斜杠
这就是我尝试过的:
APP_URL=$1
API_URL=$2
ESC_APP_URL=(${APP_URL//\//'\\/'})
ESC_API_URL=(${API_URL//\//'\\/'})
echo 'Escaped URLS'
echo $ESC_APP_URL
#Echos result: https:\\/\\/hostname.com\\/a\\/index.html
echo $ESC_API_URL
#Echos result: https:\\/\\/hostname2.com\\/test
echo "Inserting app-URL and api-URL before dist"
sed "s/tempAppUrl/$ESC_APP_URL/g;s/tempApiUrl/$ESC_API_URL/g" index.src.js > index.js
参数看起来是一样的,但是在本例中SED抛出一个错误
sed: 1: "s/tempAppUrl/https:\\/\ ...": bad flag in substitute command: '\'
谁能告诉我这里的区别吗?这些字符串看起来相同,但结果不同