2013-08-28 2 views
0

로드 밸런서 아래의 여러 웹 서버에 bash 명령을 보내는 데 사용하는 스크립트가 있습니다. 명령을 성공적으로 보낼 수 있지만 로컬로 실행하려고합니다.읽기 명령을 실행하십시오

#!/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 

그러나 내가이 작업을 어떻게합니까

[email protected]:~$ ./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 

의 결과인가?

답변

1

과 같은 명령으로 변수를 확장 할 때 :

$thisCmd 

또는이

command $thisCmd 

배쉬는 그래서 하나의 명령으로 구문 분석; 좋아하는 사람은 단지 논쟁이나 그것의 일부로 간주됩니다. html;

은 그래서 하나의 염기성 용액은 평가 사용하는 것입니다

eval "$thisCmd" 

을하지만 조금 위험하다. 원격 서버에 보내는 것과 동일합니다. 당신은 eval이 그것을하는 것처럼 그것들을 여전히 실행합니다.

관련 문제