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

相关推荐
Bug退散师40 分钟前
多路IO复用[select版TCP服务器与poll版TCP服务器]与常用网络编程函数详解
linux·服务器·c语言·网络·驱动开发·tcp/ip
xiaoye-duck43 分钟前
《Linux系统编程》Linux 系统多线程(一):线程概念(从虚拟地址空间与分页机制到优缺点解析)
linux·线程
国服第二切图仔1 小时前
05-构建与特性开关
linux·服务器·ubuntu
FREEDOM_X1 小时前
嵌入式——定时器工作原理
linux·c语言·单片机·嵌入式硬件·ubuntu
Thecozzy1 小时前
Skill和MCP和Subagent 的选择
linux·人工智能·ubuntu
硬核子牙2 小时前
代码展示大模型的智能
linux·chatgpt·agent
风曦Kisaki2 小时前
# Linux 系统资源查看命令总结(附命令快查表)
linux·运维·服务器
清水白石0083 小时前
Python 类定义阶段自动注册子类:从 `__init_subclass__` 到插件系统实战
linux·前端·python
jiang_changsheng3 小时前
Conda 的默认环境创建优先级。
linux·windows·python
smallerxuan3 小时前
Linux 设备树(Device Tree)概览
linux·设备树·dts