我有一个小脚本,用于将 bash 命令发送到负载均衡器下的多个 Web 服务器。我能够成功发送命令,但我也想在本地执行它。
#!/bin/bash
echo "Type commands to be sent to web servers 1-8. Use ctrl+c to exit."
function getCommand() {
read thisCmd
echo "Sending '$thisCmd'..."
if [ ! -z "$thisCmd" ]; then
# Run command locally
echo "From web1"
cd ~
command $thisCmd
# Send to remotes
for i in {2..8}
do
echo "From web$i..."
ssh "web$i" "$thisCmd"
done
fi
echo Done
getCommand
}
getCommand
但这导致
user@web1:~$ ./sshAll.sh
Type commands to be sent to web servers 1-8. Use ctrl+c to exit.
cd html; pwd
Sending 'cd html; pwd'...
From web1
./sshAll.sh: line 11: cd: html;: No such file or directory
From web2...
/home/user/html
我如何让这个工作?