首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以从自定义目录替换整行代码的Ejecutable脚本

问可以从自定义目录替换整行代码的Ejecutable脚本
EN

Stack Overflow用户
提问于 2020-07-23 14:40:53
回答 2查看 67关注 0票数 0

有没有办法通过脚本替换一行js代码?也许和..。我想用另一个url替换Ajax函数的url属性。我想通过脚本修改的代码行在路线上:website-project/src/client-side/js/custom-scripts.js,我想要更改的全部代码行是:

代码语言:javascript
复制
url: "https://us-central1-develop-website.cloudfunctions.net/send_contact",

to

url: "https://us-central1-production-website-prd.cloudfunctions.net/send_contact"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-23 16:33:21

使用sed,您可以这样做:

代码语言:javascript
复制
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

为了使它在脚本中更具可读性,可以借助变量对其进行分解:

代码语言:javascript
复制
#!/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"
票数 1
EN

Stack Overflow用户

发布于 2020-07-25 20:10:22

除非我搞错了,这个埃德剧本应该能做到:-

代码语言:javascript
复制
#!/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.txt
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63056897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档