MajAfy
MajAfy

Reputation: 3097

Turn off the return message from the executed command

I'm developing a bash script, I've used ssh command in my bash script to run some commands on a remote server and I need to get the result from the command which runs on the remote server. so I wrote this code:

db="$(ssh -t user@host 'mysql --user=username -ppassword -e \"SHOW DATABASES;\" | grep -Ev \"(Database|information_schema|performance_schema)\"' | grep -Ev \"(mysql)\")"

But each time which I run my bash script, I will get Connection to host closed. in first of the db result. this is a default message from ssh command.

Also, If I use > /dev/null 2>&1 end of my command the db variable would be empty.

How can I turn off the return message from the executed command?

Upvotes: 1

Views: 79

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185189

Like this :

#!/bin/bash

db=$(
    ssh -t user@host bash<<EOF
    mysql --user=username -ppassword -e "SHOW DATABASES" |
    grep -Ev "(Database|information_schema|performance_schema|mysql)" \
    2> >(grep -v 'Connection to host closed')
EOF
)

or if Connection to host closed comes from STDOUT :

...
mysql --user=username -ppassword -e "SHOW DATABASES" |
grep -Ev "(Database|information_schema|performance_schema|mysql|Connection to host closed)"
...

Upvotes: 1

Related Questions