spawn:在linux脚本中找不到命令

[tibco@c0040229 ~]$ cat test.sh.5 #!/usr/bin/expect echo -n Enter User Id: read userid echo -n Enter Password for remote user: read -s password hostname=`hostname` spawn ssh ${userid}@$hostname expect "Password: " send "${password}\r" expect "$ " { send "pbrun su - tibco\r" } expect "$ " { send "exit\r" } expect "$ " { send "pbrun bash\r" } expect "$ " { send "ps -ef |grep apache\r" expect "$ " send "exit\r" 

输出即将到来:

 [tibco@c0040229 ~]$ sh test.sh.5 Enter User Id:balp Enter Password for remote user:test.sh.5: line 7: spawn: command not found couldn't read file "Password: ": no such file or directory test.sh.5: line 9: send: command not found couldn't read file "$ ": no such file or directory couldn't read file "$ ": no such file or directory couldn't read file "$ ": no such file or directory couldn't read file "$ ": no such file or directory couldn't read file "$ ": no such file or directory test.sh.5: line 15: send: command not found 

有人可以帮我吗?

你的脚本有shebang告诉什么解释器应该运行:

 #!/usr/bin/expect 

尽管你用sh运行它。

尝试在没有shell的情况下调用它,或者使用expect:

 chmod +x script ./script 

要么

 /usr/bin/expect script 

并将其扩展名更改为不同于.sh名称,因为它不是 shell脚本,而是一个期望脚本

注:看起来实际上是半壳的一半。 所以我想这个脚本的有效版本是:

 #!/bin/sh echo -n Enter User Id: read userid echo -n Enter Password for remote user: read -s password hostname=`hostname` /usr/bin/expect << EOF spawn ssh ${userid}@$hostname expect "Password: " send "${password}\r" expect "$ " { send "pbrun su - tibco\r" } expect "$ " { send "exit\r" } expect "$ " { send "pbrun bash\r" } expect "$ " { send "ps -ef |grep apache\r" expect "$ " send "exit\r" EOF 

要么作为:

 chmod +x script ./script 

要么

 sh ./script 

如果你只想自动化你的脚本的一部分,你可以调用expecttest.sh像这样:

  #!/bin/sh echo -n Enter User Id: read userid echo -n "Enter Password for remote user:" read -s password hostname=`hostname` expect -c ' enter code here spawn ssh ${userid}@'"$hostname"' expect "Password: " send '"$password\r"' expect "$ " send "pbrun su - tibco\r" expect "$ " send "exit\r" expect "$ " send "pbrun bash\r" expect "$ " send "ps -ef |grep apache\r" expect "$ " send "exit\r" ' 

要访问从expect -c以外设置的变量,您必须使用如下所示的单引号和双引号: '"$var"'