This question already has answers here:
What is the cleanest way to ssh and run multiple commands in Bash?
                                
                                    (12个答案)
                                
                        
                                5年前关闭。
            
                    
我知道有人已经要求类似的东西,但我无法在远程主机中使用该命令。我有重击:

#!/bin/bash
IMSI="732111030120252"
ssh [email protected] bash -c " '
sed -i '61,$d' /etc/asterisk/sip.conf   #erase the lines fron 61 to end of the file sip.conf
sed -i '288,$d' /etc/asterisk/extensions.conf
echo "[IMSI$IMSI].(sets)" >> /home/openbts/Desktop/Prueba  #Write that expression inside Prueba (Ignore .)

' "


但是,他正在考虑$d()像局部变量,而不是sed和echo命令的一部分。

我发现您可以使用\\\$d或回显中的所有行,但是它们无法正常工作。

有人有主意吗?谢谢

最佳答案

您需要正确地转义字符串,尤其是$"

ssh [email protected] "sed -i '61,\$d' /etc/asterisk/sip.conf;
  sed -i '288,\$d' /etc/asterisk/extensions.conf;
  echo \"[IMSI\$IMSI].(sets)\" >> /home/openbts/Desktop/Prueba;"

关于linux - 通过ssh连接使用命令sed和echo ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23770471/

10-10 17:40