你可能想要
这边走:
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"
为了坚持heredoc,您也可以尝试
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"
为了在客户端也有变量。
由于任何原因,我无法理解 heredoc 示例。
但其他人应该看起来像
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
也得到输出。