Linux shell脚本切换为root用户执行命令

首先安装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"为换行符。

相关推荐
cyber_两只龙宝1 小时前
Nginx--企业高性能web服务器高级配置详解
linux·运维·nginx·云原生
i建模2 小时前
Omarchy设置防火墙
linux·运维
S-码农3 小时前
Linux ——条件变量
linux·开发语言
爱写代码的liding3 小时前
linux安装软件过程中报找不到某些动态链接.so文件
linux
青衫码上行3 小时前
【项目部署】Spring Boot项目部署的四种方式
java·linux·服务器·spring boot·后端·docker·腾讯云
礼拜天没时间.4 小时前
JumpServer堡垒机部署与实战:从0到1搭建统一运维入口
linux·运维·架构·堡垒机·jumpserver·sre
林姜泽樾4 小时前
linux入门第四章,cd指令和相对、绝对路径
linux·运维·服务器
jjjxxxhhh1235 小时前
[Google Test]- Google Test Ubuntu 完整验证指南
linux·数据库·ubuntu
江畔何人初5 小时前
kubernetes中configmap与secret的区别
linux·运维·云原生·容器·kubernetes
夏乌_Wx5 小时前
mybash:简易 Shell 实现的设计思路与核心模块解析
linux·服务器·前端