无效的命令名称 "cat"。期望在 shell 脚本发送 [ ]
invalid command name "cat". expect send [ ] at shell script
我想在 shell 脚本中使用 expect
我的代码在这里
#!/bin/sh
expect << EOF
send [cat hello]
EOF
此命令失败
invalid command name "cat"
while executing "cat hello"
invoked from within "send [cat hello]"
但是,在 expect 提示符下发送 [cat hello] 命令成功
expect1.1> send [cat hello]
world
expect1.2>
为什么我输出的执行结果不同?
里面expect
shell:
当你在 expect
shell 时,只要给出任何可执行的 shell 命令,它就会被执行,就像你在终端中正常执行它一样。
expect1.1> ls
A B C D
expect1.2> cat A
I am A
expect1.3> pwd
/home/dinesh/test
expect1.4>
如果你像 [cat A]
一样将它们放在方括号内,与普通的命令调用不同,它仍然会执行 shell 命令和 returns 空字符串。
expect1.4> set val [cat A]
I am A
expect1.5> puts "->$val<-"
-><-
expect1.6>
如果您添加的过程与任何 shell 命令的名称相同,则只会调用相应的命令。
expect1.6> proc cat {input} {
+> return "you passed $input\n"
+> }
expect1.7> cat A
you passed A
expect1.8> cat
wrong # args: should be "cat input"
while executing
"cat "
expect1.9> send [cat A]
you passed A
expect1.10>
除非 spawn_id
被设置(通过生成任何程序),Expect
将始终期待并分别向 stdin
和 stdout
发送命令。
expect1.11> exp_internal 1
expect1.12> expect hai
expect: does "" (spawn_id exp0) match glob pattern "hai"? no
如您在调试输出中所见,exp0
仅指向 stdin
。
当expect
程序调用时
当 expect
程序被显式调用时,那时,为了执行任何 shell 命令,你必须使用 exec
命令,否则你将得到 invalid command name error
消息。
expect << EOF
send [exec cat hello]
EOF
输出:
[dinesh@lab test]$ ./baek.sh
I am A
[dinesh@lab test]$
现在,为什么会有这种差异?
从技术上讲,当您使用 expect
shell 时,您可以在其中执行 shell 命令。
我想在 shell 脚本中使用 expect
我的代码在这里
#!/bin/sh
expect << EOF
send [cat hello]
EOF
此命令失败
invalid command name "cat"
while executing "cat hello"
invoked from within "send [cat hello]"
但是,在 expect 提示符下发送 [cat hello] 命令成功
expect1.1> send [cat hello]
world
expect1.2>
为什么我输出的执行结果不同?
里面expect
shell:
当你在 expect
shell 时,只要给出任何可执行的 shell 命令,它就会被执行,就像你在终端中正常执行它一样。
expect1.1> ls
A B C D
expect1.2> cat A
I am A
expect1.3> pwd
/home/dinesh/test
expect1.4>
如果你像 [cat A]
一样将它们放在方括号内,与普通的命令调用不同,它仍然会执行 shell 命令和 returns 空字符串。
expect1.4> set val [cat A]
I am A
expect1.5> puts "->$val<-"
-><-
expect1.6>
如果您添加的过程与任何 shell 命令的名称相同,则只会调用相应的命令。
expect1.6> proc cat {input} {
+> return "you passed $input\n"
+> }
expect1.7> cat A
you passed A
expect1.8> cat
wrong # args: should be "cat input"
while executing
"cat "
expect1.9> send [cat A]
you passed A
expect1.10>
除非 spawn_id
被设置(通过生成任何程序),Expect
将始终期待并分别向 stdin
和 stdout
发送命令。
expect1.11> exp_internal 1
expect1.12> expect hai
expect: does "" (spawn_id exp0) match glob pattern "hai"? no
如您在调试输出中所见,exp0
仅指向 stdin
。
当expect
程序调用时
当 expect
程序被显式调用时,那时,为了执行任何 shell 命令,你必须使用 exec
命令,否则你将得到 invalid command name error
消息。
expect << EOF
send [exec cat hello]
EOF
输出:
[dinesh@lab test]$ ./baek.sh
I am A
[dinesh@lab test]$
现在,为什么会有这种差异?
从技术上讲,当您使用 expect
shell 时,您可以在其中执行 shell 命令。