技术栈

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

红色石榴2024-03-27 12:54

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

上一篇:刷题日记——济南大学机试
下一篇:冒泡排序的习题全集(含答案)
相关推荐
V__KING__
5 小时前
systemd-remount-fs,fstab之间的渊源
linux·服务器·网络
酿情师
5 小时前
Windows Subsystem for Linux (WSL, Ubuntu)安装教程(详细)
linux·windows·ubuntu
Titan2024
6 小时前
Linux环境变量个人笔记
linux·服务器·c++
青柠代码录
6 小时前
【Linux】路径区分:testdir、testdir/、testdir/*
linux·运维·服务器
7yewh
6 小时前
jetson_yolo_deployment 02_linux_dev_skills
linux·python·嵌入式硬件·yolo·嵌入式
supersolon
7 小时前
Windows下WSL(Ubuntu24.04)安装Nodejs
linux·ubuntu·node.js
进击切图仔
7 小时前
跨系统时间戳同步问题解决总结
linux
wsoz
8 小时前
文件IO讲解
linux·嵌入式
mounter625
8 小时前
基于MLX设备的Devlink 工具全指南与核心架构演进
linux·运维·服务器·网络·架构·kernel
热门推荐
01GitHub 镜像站点02Qwen3.5 开源全解析:从 0.8B 到 397B,代际升级 + 全场景选型指南03OpenClaw 使用和管理 MCP 完全指南04OpenClaw Control UI安全上下文访问配置05UV安装并设置国内源06本地部署 OpenClaw + DeepSeek-R1 完全指南07OpenClaw macOS 完整安装与本地模型配置教程(实战版)08Openclow安装保姆级教程09在 Windows 上配置 Claude Code从安装到解决网络问题10Claude Code + GLM4.7 避坑指南:解决 Unable to connect to Anthropic services