shell腳本之通過發送帶 字符串或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工具在一般發行版中默認沒有安裝,需要手工安裝,這裏就不演示了。
shell腳本之通過發送帶\n字符串或expect腳本實現交互輸入自動化