C/C++|shell调用expect


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



【C/C++|shell调用expect】

    推荐阅读