有没有办法通过脚本替换一行js代码?也许和..。我想用另一个url替换Ajax函数的url属性。我想通过脚本修改的代码行在路线上:website-project/src/client-side/js/custom-scripts.js,我想要更改的全部代码行是:
url: "https://us-central1-develop-website.cloudfunctions.net/send_contact",
to
url: "https://us-central1-production-website-prd.cloudfunctions.net/send_contact"发布于 2020-07-23 16:33:21
使用sed,您可以这样做:
sed -i 's/url: "https:\/\/us-central1-develop-website.cloudfunctions.net\/send_contact"/url: "https:\/\/us-central1-production-website-prd.cloudfunctions.net\/send_contact"/' website-project/src/client-side/js/custom-scripts.js为了使它在脚本中更具可读性,可以借助变量对其进行分解:
#!/bin/bash
from='url: "https://us-central1-develop-website.cloudfunctions.net/send_contact"'
to='url: "https://us-central1-production-website-prd.cloudfunctions.net/send_contact"'
target_file=website-project/src/client-side/js/custom-scripts.js
sed -i "$(printf 's+%s+%s+' "$from" "$to")" "$target_file"发布于 2020-07-25 20:10:22
除非我搞错了,这个埃德剧本应该能做到:-
#!/bin/sh
cat >> edrep.txt << EOF
/develop-website/ # This command will get you to the line with this string.
s/develop-website/production-website-prd/ # This will modify the line for you.
wq # This writes to the file and quits.
EOF
ed -s website-project/src/client-side/js/custom-scripts.js < edrep.txt
rm -v ./edrep.txthttps://stackoverflow.com/questions/63056897
复制相似问题