在 expect 脚本中从用户那里读取变量

reading a variable from the user in an expect script

下面是我的脚本:

#!/usr/bin/expect
echo "enter unique id" 
read test
mkdir -p  /evoting_test/$test
        spawn scp -r abc@10.150.10.104:/shareddata/was/l1/*.pdf /rishabh/$test/
        set pass "abc123"
        expect {
                password: {send "$pass\r"; exp_continue}
                }

我遇到错误:

invalid command name "echo"
    while executing
"echo "enter uNIQUE id" "
    (file "./scp_test.sh" line 2)

它不是从用户读取变量并在命令中使用该变量

恕我直言,解决这个问题的最佳方法是将两个文件分开,每个文件都根据其要求使用不同的解释器。

  1. 第一个文件:将包含将所有 pdf 文件复制到所需位置的代码,您也可以在此脚本中读取目标位置。

  2. 对上述文件使用 expect 运行 并完成您的工作。

"expect" 方法是:

send_user "enter unique id: "
expect_user -re "(.*)\n"
set test $expect_out(1,string)
send_user "you said $test\n"

由于 expect 扩展 ,您可以使用:

puts -nonewline "enter unique id: "
flush stdout
gets stdin test
puts "you said $test"

此外,您会收到 mkdir 的错误信息——这是一个外部命令。您可以执行以下操作之一:

exec mkdir -p /evoting_test/$test   # invoke the external command
file mkdir /evoting_test/$test      # use the builtin

http://tcl.tk/man/tcl8.6/TclCmd/file.htm