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"为换行符。

相关推荐
wacsii31 分钟前
【Linux进程(五)】进程控制全解析:从创建、终止、等待到程序替换
linux·服务器
七夜zippoe1 小时前
Nexent Linux 部署:用 deploy.sh
linux·服务器·docker·部署·ai智能体·nexent
去码头整点薯条981 小时前
NFS服务器
linux·运维·服务器
蜗牛一步步往上爬2 小时前
第一章 初识Linux shell
linux·运维·服务器
拂拉氏2 小时前
【知识讲解】 Linux线程中锁的认识
linux·互斥锁
无足鸟ICT2 小时前
【RHCA+】系统调优配置文件
linux·服务器
时间的拾荒人4 小时前
Shell 脚本核心语法详解(下篇):流程控制、函数、重定向与文件包含
linux·运维·服务器·bash·脚本编程
蜗牛一步步往上爬5 小时前
第四章 更多的bash shell命令
linux·开发语言·bash
进击的荆棘6 小时前
Linux网络——网络基础概念
linux·运维·服务器·网络
.YM.Z6 小时前
从操作系统进入Linux进程概念及进程状态
linux·运维·服务器