前言
在Mac下想快速实现重启,而无需在sudo中再次输入密码,可以利用expect实现sudo中自动输入password。
前期准备
- brew
- expect 通过brew安装命令
brew install expect
代码
- 在设置密码值的地方写入你的密码
#!/usr/bin/expect
、
#设置密码的值
set password "你的用户密码"
#执行重启脚本
spawn sudo reboot
#判断是否包含Password字符串
expect "Password"
#send \n相当于回车
send "$password\n"
#退出expect返回终端
interact
- 将以下代码保存为.sh
- sudo chmod a+x “你的sh文件”
- 对sh文件右键简介中删除你的文件名后缀
双击运行即可使用了