用于替换自定义目录中的整行代码的可执行脚本

Ejecutable script to replace an entire line of code from custom directory

有什么办法可以通过脚本替换一行js代码吗?。也许用 sed...我想用另一个 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"

使用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"

除非我弄错了,否则这个 Ed 脚本应该可以解决问题:-

#!/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