如何使用 sed 将整行替换为字符串作为变量?
#!/bin/bash
ssh $1 ssh-keyscan -t rsa $1 > /tmp/$1
RSA=$(cat /tmp/$1)
echo $RSA
sed -i 's:'^"$1".*':'"$RSA"':' /etc/ssh/ssh_known_hosts
cat /etc/ssh/ssh_known_hosts | grep $1
它是将变量存储在 RSA 中而不是替换,不确定 sed 部分有什么问题。
最佳答案
您可以使用以下命令。
#!/bin/bash
ssh $1 ssh-keyscan -t rsa $1 > /tmp/$1
RSA=$(cat /tmp/$1)
echo $RSA
sed -i -e "/$1/ d" -e "/^$1/ a $RSA" /etc/ssh/ssh_known_hosts
cat /etc/ssh/ssh_known_hosts | grep $1
我根据您的要求进行了修改,如果行不存在则添加,如果存在则替换。
关于linux - 如何用字符串作为变量sed替换整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46582038/