expect的作用不再累述
下面是一个简单的shell调用expect脚本的例子
作用是从一个普通用户切换到root用户并做相关操作(删除root家目录下的myfile),然后切换回来
shell 脚本: test.sh
#!/bin/sh
echo "test expect"
User=root
Passwd=mypass
File=myfile
./my.exp $User $Passwd $File
echo "test end"
expect 脚本:my.exp
#!/usr/bin/expect -f
set User [lindex $argv 0]
set Passwd [lindex $argv 1]
set File [lindex $argv 2]
spawn su - $User
expect "*password*"
send $Passwd
send "\n"
expect "*$"
send "rm -rf $File"
send "\n"
expect "*$"
send "exit\n"
expect eof
exit
本文介绍了一个使用Expect脚本来简化从普通用户切换到root用户的流程,并执行特定操作(如删除文件)后再切换回普通用户的例子。通过一个具体的Shell脚本和Expect脚本的配合使用,展示了如何自动完成这一系列任务。
995

被折叠的 条评论
为什么被折叠?



