首先安装expect。
sudo apt install expect
创建shell脚本文件,示例内容如下:
bash
#!/usr/bin/expect
spawn su root
expect {
"密码:" {send "00000\r"}
"Password:" {send "000000\r"}
}
send "./user_app\r"
expect eof
exit
其中,"000000\r"是root用户密码。
send语句用于向终端传递需要输入的字符串。
如send "000000\r"是输入密码
send "./user_app\r"是输入需要执行的应用程序。"\r"为换行符。