我写了一个bash脚本,列出了一些服务器/用户名。我选择了一个#,然后使用我的用户名将我连接到该服务器。到目前为止,该脚本可以正常运行,而事实是我启动ssh时,bash脚本挂起。它不会把我扔进ssh。

#!/bin/bash
echo `clear`
SERVER1="1.) Server1/username1"
SERVER2="2.) Server1/username2"
echo -e "Please choose a server:"
echo $SERVER1
echo $SERVER2
read server
if [ $server -eq 1 ]; then
        serverconnect="ssh -t [email protected]"
        servername="server1.com"
        serveruser="username1"
else

        if [ $server -eq 2 ]; then
                serverconnect="ssh -t [email protected]"
                servername="server1.com"
                serveruser="username2"
        fi
fi

echo "Connecting you to: $servername as $serveruser"
echo `$serverconnect`

最佳答案

只需正常执行ssh。不要把它放在变量里面

if [ $server -eq 1 ]; then
        serverconnect="[email protected]"
        servername="server1.com"
        serveruser="username1"
else

        if [ $server -eq 2 ]; then
                serverconnect="[email protected]"
                servername="server1.com"
                serveruser="username2"
        fi
fi

ssh -t "$serverconnect"

10-04 12:06