gpt4 book ai didi

bash - 在 sed 命令中转义正斜杠

转载 作者:行者123 更新时间:2023-11-29 08:42:31 24 4
gpt4 key购买 nike

<分区>

使用 BashSED 我试图用 URL 替换 js 文件中的两个字符串。

应该插入的两个 url 是我运行 .sh 脚本时的输入参数。

./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: '\'

谁能告诉我这里的区别?字符串看起来相同但给出不同的结果。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com