用于sed替换的私钥中的转义字符

时间:2014-07-21 23:39:59

标签: regex bash sed

我需要将config.yaml文件中的字符串$ ssh_private_key_contents替换为实际的ssh私钥内容(键的每一行用\ n分隔)。

$ssh_private_key_contents="-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAvx9xuVc8eq2ug7OgUKQHO2YNgw2qxPN6HyAVRtCb6E8rolnI\nuSnE/CxuNMk1E4+oD+qkL772FXgL656DvKTnXpz3pySFj/KppNmm3chmMQGcXqyV\nQcYyXy/vpKgh4gBYmEi5IM9ksDtAby2OPD089w3gOVFhgvg5307pW4o5ccbmBUub\nB1QNvWYiCB7Po8FLVHq+a5f2bg1ui1IY10F93ej555Bzg/woE9jqPGKBL9iWV0M4\nulUVW42Q4V0lJiyFo724KD30N6zymMQ4SMWExW3BmJRWGtyvWY8hTxHKiuToyywF\nga5AxWaApg/+0T1CKBgypfviOacaKg5PYt050wIDAQABAoIBAH0L1r18HT7q6haT\nbR215GFxqP+I7qVD5C//LsyRaqB5RERJ2DGF6bNQYp640rhEsqIPVHNEzv9G9w2n\nPewsPJFDxCkglR6Ul3gbS7kqB+qicivjYRiS05xB4dpqgxi6Pz0Lv8x+xRMlAUh8\nluZE7uF5u63k2Bb3z+2SVM4mxuTwKpqPmcex/kuHDUdEq0FgHLpBFxXpUwoPjb1J\nCH5RwpU/VVSaEWws2UauXFPYNUJVY0aGPm4id2k3gBWTpvV8ATjw33cgvx13hD+8\nT98tMzAw6Tv6N6gIhrrmWNAA42CcfMxgrfVM6OxhaNkMk7/FaodEBNTMZnwMuvQt\nOyimXbkCgYEA7Ybh2REvjWuZ3xXOe4ei+SmF5f7+XyGwZBbmVWTEzi1Wozhf16Wx\nFkGk5JcnVn/Pl1rfXPBFyrb7D2joe1SQUVY4CK1gVnO2WgAziOVg3Yq9p6hG0m7O\nXLoMckN4Ekt97x4pXmKrfSDuIYTlM0DELtST5MrMSFaVmxLXDce4708CgYEAzfyq\nY9UIYBIlhkJ3GLMA22MAKDYNjuVTHQqOoZsDCUkqr1r+lvKGYblqr5s70R/FbTjJ\n00wa7KYGCnz6D26qMD/TZ319ySTyY4M64GIswFHt/J6H8RgTRgXyv3AI0Yv41T1t\nxQzhO9cuCkbuw99ejDbr/VL14niXysbOkLUDjD0CgYAdvqljtHo+/H2NaT6tS4QG\nLcP91Fvob+CV+CQqB2953kWeNP4TlEVD4z6R/bfIZkMJ0jBshA5JX3a7LEkIwMM9\n3E0yEPwid2UbijDp8Meblrc/Wpxb19Jx3D6YxIgxylBf9Gf70OFmzwvjGLBXJcIr\nlyVaHmdMm8MqY0aZDCzFkwKBgBmPNIVWtcdV+RoDvUtvSrfuPOLAckY85mdAHBqp\nZWMNlSJVJCoOxeXyX+6SUIXOb2zyEvkJBH5Ay5cZkt2/WZbLFKxOwgDneLwoWHHn\nPK5QfDtQYIXl+t5BdrGScUWvHJ05FhnRU1FdjfTwgEgFu3Gijv3MAX6sgAlFSfRi\nwaHxAoGAfLSIH2rUBrVMjFkPsQY1Wg/8lAK0WqM5md19w3aSQREQw/eSAUgwNtw7\nUlDxeC8EfEM65BgqO1Oo3H8vabMRqd55L3Cb/Nb/tYKUG90DeBTOO8CC0RPtD2CS\nqNUWH8qPzaXEJ74ADUupv5ukkpA6zWVg9e4VNDG0cpwZmRBcSIY=\n-----END RSA PRIVATE KEY-----\n"

sed -i 's/$ssh_private_key_contents/'"$ssh_private_key_contents"'/g' config.yaml

我收到了这个错误:

sed: -e expression #1, char 81: unknown option to `s'

我试图逃避/但不断收到更多错误。实际的配置文件有不同类型的需要填充的字符串。

1 个答案:

答案 0 :(得分:5)

正如ymonad在评论中所说,假设{私有密钥}中没有出现%,您可以使用:

sed -i "s%\$ssh_private_key%$ssh_private_key%" config.yaml

或者,选择$ssh_private_key的内容中未出现的任何其他字符。

另外,请考虑是否要转义输出中的\,或者是否希望\n成为输出中的真实换行符。