开发者

How to remotely edit a line containing double quotes and variables using sed across ssh?

I have a sed command that works perfectly if I run it locally on Ubuntu or our embedded client Arago:

sed -i 's/export PART="$1"/export PART="A"/' flash.sh

This results in exactly what I need on both versions of sed, a line that changes:

export PART="$1"

to

export PART="A"

My problem is, I need to run that same command across a network to the embedded client so I tried this on an Ubuntu server using bash, to an Arago client using sh:

ssh -n -o stricthostkeychecking=no root@10.14.150.113 sed -i 's/PART="$1"/PART="A"/' flash.sh

Which results in a line that contains:

export PART=A"$1"

The substitution command needs to stay inside the single quotes so the double quotes are passed as literals, or maybe there's a better way to keep the double quotes in the two strings? This looks to me like the $1 is simply being ignored like it's empty and the "A" is being passed as A and replacing the end of PART=. I've tried encapsulating the command in single and do开发者_开发知识库uble quotes, which both result in the same thing. I've also tried escaping the quotes with backslashes, same result. I think this is something with quote expansion with sh that I simply don't understand. Or possibly something I don't understand with ssh.

I've read through a number of other threads that are similar, but none of them dealt with using ssh to run the command remotely. I'm no longer a novice at sed, but this one has become quite the puzzle for me.


ssh -n -o stricthostkeychecking=no root@10.14.150.113 'sed -i "s/export PART=\"\\\$1\"/export PART=\"A\"/" flash.sh'


You can switch back and forth to get the quoting you need. try this

ssh -n -o stricthostkeychecking=no root@10.14.150.113 sed -i 's/PART='"$1"'/PART="A"/' flash.sh
# -------------------------------------------------------------------^^^^^^

OR if you really need the value of $1 surrounded by dbl-quotes,

ssh -n -o stricthostkeychecking=no root@10.14.150.113 sed -i 's/PART='\"$1\"'/PART="A"/' flash.sh
# -------------------------------------------------------------------^^^^^^^^

You do want $1 to come from your local machine (not the ssh'd to machine), right?

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜