2010-11-18 177 views
1

在下面的expect腳本如何獲得在expect腳本 if [$? -eq 0]的rsync的退出狀態拋出一個錯誤Expect腳本執行狀態

  invalid command name "$?" 
     while executing 
     "$? -eq 0 " 
     invoked from within 
     "if [ $? -eq 0 ]" 
     (file "./tmp544.sh" line 7) 


    #!/usr/bin/expect -f 

    eval spawn ssh -l root 174.54.87.12 rsync /root/project /usr/project 
    expect "[email protected]'s password: $" 
    send "\xxxxxxx\n" 
    expect "\\$ $" 
    if [ $? -eq 0 ] 
    then 
     echo "11" 
    else 
     echo "22" 
    fi 

回答

2
if [$? -eq 0] 

是一個殼結構,和你在一個期望腳本。 我認爲你應該rsync的部分從SSH部分分開,並使用遠程shell評估rsync的狀態:

eval spawn ssh -l root 174.54.87.12 
expect "[email protected]'s password: $" 
send "\xxxxxxx\n" 
expect "\\$ $" 
send "rsync /root/project /usr/project" 
expect "\\$ $" 
send "echo $?" 
# Examine the output of $? 

$?在交互式shell中可用,不僅在腳本中,您可以在命令行中使用它,並且它看起來是一個很好的機會!

我對期待並不熟悉,但我猜期待「\ $ $」正在等待shell提示符,所以我一味複製粘貼它。

+0

期望{\ $ $}是一種可以說是更符合習慣的替代方式,期望「\\ $ $」 – 2010-11-21 15:12:14