Как передать длинную строковую (с новыми строками) переменную в качестве среды для команды ssh - PullRequest
0 голосов
/ 07 ноября 2019
KEY=$(cat ~/.ssh/id_rsa.pub)
ssh root@127.0.0.1 KEY="$KEY" echo "hi"

Это приведет к ошибке и отобразит ошибку примерно так:

bash: AAAAB3NzaC1yc2EAAAADAQABAAABAQDJ65FHdb8FyD...tpFnRUSZ: No such file or directory

как я могу это исправить?

1 Ответ

0 голосов
/ 07 ноября 2019

Предполагается, что используется bash:

В более общем случае, когда входные данные могут содержать произвольные символы, включая кавычки, рассмотрите возможность использования (специфичного для bas) ${var@A} для создания назначения:

ssh root@localhost ${KEY@A} echo 'hi'

Обновление 1: справочная страница bash (версия 4.4): в операции '@A' (и других):

  ${parameter@operator}
          Parameter  transformation.   The  expansion  is either a transformation of the value of parameter or information
          about parameter itself, depending on the value of operator.  Each operator is a single letter:

          Q      The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
          E      The expansion is a string that is the value of parameter with backslash escape sequences expanded as with
                 the $'...' quoting mechansim.
          P      The  expansion  is a string that is the result of expanding the value of parameter as if it were a prompt
                 string (see PROMPTING below).
          A      The expansion is a string in the form of an assignment statement or declare command that,  if  evaluated,
                 will recreate parameter with its attributes and value.
          a      The expansion is a string consisting of flag values representing parameter's attributes.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...