您的位置:首页 > 运维架构 > Shell

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

2017-06-19 23:25 816 查看
编写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工具在一般发行版中默认没有安装,需要手工安装,这里就不演示了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: