shell脚本之通过发送带\\n字符串或expect脚本实现交互输入自动化

shell脚本之通过发送带\n字符串或expect脚本实现交互输入自动化

编写shell脚本难免遇到需要交互式输入指令的步骤:

方法一:

# cat action.sh 

#!/bin/sh

read -p ”enter number:” no;

read -p ”enter number:” name;

echo you have entered $no,$name;

# sh action.sh 

enter number:chavin

enter number:king

you have entered chavin,king

# echo -e ”chavin\nking\n” | sh action.sh 

you have entered chavin,king

或者

# echo -e ”chavin\nking\n” > input.data

# cat input.data 

chavin

king

# sh action.sh < input.data 

you have entered chavin,king

案例1:使用交互式自动化为用户改密码:

# useradd chavin0101

# echo -e ”dbking588\ndbking588\n” | passwd chavin0101

Changing password for user chavin0101.

New password: Retype new password: passwd: all authentication tokens updated successfully.

方法二:

使用expect实现交互式任务自动化,expect工具在一般发行版中默认没有安装,需要手工安装,这里就不演示了。

WWW.OOCOLO.COM  欧欧colo教程网  美国站群服务器 美国服务器 优惠推荐 

SEO,网站建设,服务器使用教程分享

请自行测试,如有问题请联系 www.oocolo.com

相关文章

在Docker中,挂载目录的权限问题通常可以通过以下方法解决:...
在Ubuntu命令行中设置中文可以通过以下几种方式实现: 安装...
要查看CentOS中的重启记录,可以使用以下命令: 使用last命...
要判断哪个串口在工作,可以通过以下方法: 使用命令行工具...
要查看Linux系统中数据盘的挂载点,可以按照以下步骤进行: ...
要更改Ubuntu系统的启动项顺序,可以按照以下步骤操作: 打...