あなたはしたいかもしれない
- すぐにコマンドを実行し、
- 実行することになっているクエリを指定します。
こちらです:
example=$(ssh root@SERVER.com mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e '"SELECT now();"')
echo "My goal is to read the variable here: $example"
行く方法です。
両方の場所で本当に必要な場合は、サーバーにスクリプトを配置するか、試すことができます
example=$(ssh root@SERVER.com 'example=$(mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"
ヒアドキュメントに固執するために、試してみることもできます
example=$(ssh root@SERVER.com << EOFMARK
example=$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")
echo "Variable is $example"
EOFMARK)
echo "My goal is to read the variable here: $example"
クライアント側にも変数を持たせるために。
なんらかの理由で、ヒアドキュメントの例に頭を悩ませることができません。
しかし、他の人は次のように見えるはずです
example=$(ssh root@SERVER.com 'mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"')
echo "My goal is to read the variable here: $example"
出力をローカル変数に正確に与える、または
example=$(ssh root@SERVER.com 'example=$(mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"
ただし、ここでは、ローカル出力が次のようになることに注意してください (例)
My goal is to read the variable here: Variable is 2014-03-07 20:42:23
サブ文字列Variable is
も出力されるためです。