简体   繁体   English

如何使用 sed 通过 ssh 修改远程文件?

[英]How can i modify a remote file over ssh with sed?

i am trying to add a line to the top of a JS file on a remote server the command i want to run is:我正在尝试在远程服务器上的 JS 文件顶部添加一行,我要运行的命令是:

sed -i "1i\const test = require(\'../../../test/test.json\');" /opt/test.js

so i have tried the following:所以我尝试了以下方法:

ssh user@host "sed -i "1i\const test = require(\'../../../test/test.json\');" /opt/test.js"

this gives me an error due to the "(" and ")" so i added a "\" before them:由于“(”和“)”,这给了我一个错误,所以我在它们之前添加了一个“\”:

ssh user@host "sed -i "1i\const test = require\(\'../../../test/test.json\'\);" /opt/test.js"

however i still get the error:但是我仍然收到错误:

bash: -c: line 0: syntax error near unexpected token `('

how can i fix this?我怎样才能解决这个问题?

Rather than use sed -i (which just manages a temp file behind the scenes), use ed and provide the script (via ssh ) to ed 's standard input.与其使用sed -i (它只是在后台管理一个临时文件),不如使用ed并将脚本(通过ssh )提供给ed的标准输入。

ssh user@host 'ed /opt/test.js' <<'EOF'
1i
const test = require('../../../test/test.json');
.
wq
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM