楼主遇到一个这样的场景:

在服务端执行脚本的时候,需要临时用到root权限,但是通过sudo 来执行命令需要输入密码,解决方法如下:

shell脚本嵌套执行expect命令可以实现:



#!/bin/bash

/usr/bin/expect <<EOF

#使用root权限来执行命令
spawn sudo chmod -R 777 /test
expect "word"
send "pass\r"
expect eof

EOF