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

相关推荐
Y多了个想法15 小时前
Linux驱动开发与Android驱动开发
android·linux·驱动开发
大锦终15 小时前
【Linux】TCP协议
linux·运维·tcp/ip
任风雨17 小时前
附录I Linux命令一览
linux·服务器
黄金旺铺17 小时前
Linux 命令与运维终极手册(2025 完整版)
linux·运维·服务器
qq_3985865417 小时前
小小电脑安装logisim-evolution
linux·proot
oioihoii18 小时前
不止于Linux:百花齐放的开源世界与社区的力量
linux·运维·开源
打不了嗝 ᥬ᭄18 小时前
传输层协议TCP
linux·服务器·网络·c++·tcp/ip
moringlightyn18 小时前
基础开发工具--编译器g++/gcc 自动化构建make/Makefile
linux·运维·笔记·自动化·c·编译器·make/makefile
程序员陆通19 小时前
CentOS/AlmaLinux 9 中 SSH 服务启动失败:OpenSSL 版本不匹配解决
linux·centos·ssh
ZYMFZ19 小时前
HAProxy 简介及配置
linux·负载均衡·haproxy