我寫了一個期望腳本,它有助於在遠程計算機中執行命令。當執行完成後,我想獲取用戶輸入的一條線,然後將其發送到遠程的bash,這裏是代碼片段:獲取一行用戶輸入,然後將其作爲Bash命令執行
#! /usr/bin/env expect
...
spawn ssh -l $user $host
...
send_tty -- "Enter your command: "
set timeout -1
# match only printable characters (prevent from pressing TAB)
expect_tty eof exit -re {([[:print:]]*)\n}
send_tty -- "\n"
set timeout 10
# send the command to remote shell
send "$expect_out(1,string)"
expect "$GENERAL_PROMPT"
然而,如果輸入的是一樣的東西:ls /"
,我的程序將被阻止,因爲遠程shell期望通過提示字符串「>」來獲得更多字符。其實,我希望bash將不提示輸入更多,而不是隻打印錯誤消息:
$ read COMMAND
ls /"
$ eval "$COMMAND"
bash: unexpected EOF while looking for matching `"'
bash: syntax error: unexpected end of file
我可以在我的腳本實現這一目標?
@ zhujs:我不是專家慶典,但是,當我試圖簡單地執行'和'LS讀COMMAND' /「'作爲輸入和'EVAL $ COMMAND'我在得到上面的錯誤如果輸入沒有雙引號,即'ls /',那麼它工作正常 – Dinesh
我只想讓我的腳本在用戶輸入'ls /'時打印同樣的錯誤' – zhujs
使用'eval'就好像你試過了。 – pynexj