我正在尝试将用户提供的用户名存储在变量中,然后使用该用户名通过ssh登录服务器。
这似乎有效
amit@sharknado:~$ read -p "username: " username; ssh -l $username localhost
username: foobartest
foobartest@localhost's password:
但是,当我对/bin/sh -c
执行相同操作时,$username
似乎没有存储?
amit@sharknado:~$ /bin/sh -c "read -p \"username: \" username; ssh -l $username localhost"
username: foobar
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port]
[-Q cipher | cipher-auth | mac | kex | key]
[-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] [user@]hostname [command]
amit@sharknado:~$
答案 0 :(得分:1)
发现我只需要使用-c
参数的单引号而不是双引号
amit@sharknado:~$ /bin/sh -c 'read -p "username: " username; ssh -l $username localhost'
username: testfoo
testfoo@localhost's password: