我写了一个期望脚本,它有助于在远程计算机中执行命令。当执行完成后,我想获取用户输入的一条线,然后将其发送到远程的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