编写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
king
" | sh action.sh
you have entered chavin,king
或者
# echo -e "chavin king " > input.data
# cat input.data
chavin
king
# sh action.sh < input.data
you have entered chavin,king
案例1:使用交互式自动化为用户改密码:
# useradd chavin0101
# echo -e "dbking588
dbking588
" | passwd chavin0101
Changing password for user chavin0101.
New password: Retype new password: passwd: all authentication tokens updated successfully.
方法二:
使用expect实现交互式任务自动化,expect工具在一般发行版中默认没有安装,需要手工安装,这里就不演示了。