Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

558
Visualizações
bash variable seems to fail assignment to output of psql executed in container

I have Postgres running in a docker container on a remote host. I'm attempting to ssh to that remote host, and use a heredoc to pass a script to that shell. In that script, I attempt to execute a psql query against the postgres database that resides in the container on that host, and assign the output to a variable. I'm sorry, I know that's a bit convoluted, but maybe some code will be more coherent (nor not)

If I run this, I get nothing out of my "echo" of $dbVersion... in fact, due to some other experimentation, I'm fairly convinced it never gets to the "echo" line at all, but for the life of me I don't know why, because...

ssh root@$serverIP13 <<EOF
postgresId=\$(docker ps | grep 'postgres_db' | awk '{print \$1}')
dbVersion=\$(docker exec -i \$postgresId psql -X -A -d myDb  -U myUser -t -c "SELECT Max(Version) FROM DbVersion;")
echo \$dbVersion
EOF

...after turning and twisting my code this way and that... I finally found a way that works, and I get a successful "echo" of my version number. I'm quite new to bash, so I'm hoping someone out there can discern this mystery for me and help me understand a bit better how bash works in this regard, and why the below succeeds, and the above fails.

ssh root@$serverIP13 <<EOF
postgresId=\$(docker ps | grep 'postgres_db' | awk '{print \$1}')
dbVersion=\$(echo "psql -X -A -d myDb -U myUser -t -c 'SELECT Max(Version) FROM DbVersion;'" | docker exec -i \$postgresId bash)
echo \$dbVersion
EOF
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The normal way to get a docker exec -i output is to call for docker to run a shell (which, in turn, runs your command.
See "Bash / Docker exec: file redirection from inside a container" (I mentioned in 2015)

dbVersion=\$(docker exec -i sh -c '\$postgresId psql -X -A -d myDb  -U myUser -t -c "SELECT Max(Version) FROM DbVersion;"')
over 4 years ago · Santiago Trujillo Relatório

0

Following script worked without invoking bash :

ssh root@$serverIP13 << 'EOF'
postgresId=$(docker ps | grep 'postgres_db' | awk '{print $1}')
dbVersion=$(docker exec $postgresId psql -X -A -d postgres -U postgres -c "select current_database()")
echo $dbVersion
EOF

I use 'EOF' to avoid having to escape.

To pass local values to remote :

ssh root@$serverIP13 /bin/bash -s 1 2 3 << 'EOF'
echo $1 $2 $3
EOF

You should not use -i if you put docker exec in $(...)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda