2.查看expect的路径,可以用
[zhuying@SOR_SYS opt]$ which expect
/usr/bin/expect
[zhuying@SOR_SYS opt]$ view autosu.sh
3。确定脚本有可执行权限
chmod +x autosu.sh
#!/usr/bin/expect -f //这个expect的路径就是用which expect 查看的结果
spawn su - Oracle //切换用户
expect "password:" //提示让输入密码
send "99billzy\r" //输入oracle的密码
interact //操作完成
这在root用户切换到oracle用户的时候特别的快,但是在其他用户切换到oracle的时候则需要等待一段时间!
另外需要注意的是:
不能按照习惯来用sh autosu.sh来这行expect的程序,会提示找不到命令,如下:
autosu.sh: line 3: spawn: command not found
couldn't read file "password:": no such file or directory
autosu.sh: line 5: send: command not found
autosu.sh: line 6: interact: command not found
因为expect用的不是bash所以会报错。执行的时候直接./autosu.sh就可以了。~切记!