shell脚本系列:read用法

选项说明

read [-options] [variables]
选项 说明
-a array 把读取的数据赋值给数组 array,从下标 0 开始。
-d delimiter 用字符串 delimiter 指定读取结束的位置,而不是一个换行符(读取到的数据不包括 delimiter)。
-e 在获取用户输入的时候,对功能键进行编码转换,不会直接显式功能键对应的字符。
-n 读取 num 个字符,而不是整行字符。
-p prompt 显示提示信息,提示内容为 prompt。
-r 原样读取(Raw mode),不把反斜杠字符解释为转义字符。
-s 静默模式(Silent mode),不会在屏幕上显示输入的字符。当输入密码和其它确认信息的时候,这是很有必要的。
-t seconds 设置超时时间,单位为秒。如果用户没有在指定时间内输入完成,那么 read 将会返回一个非 0 的退出状态,表示读取失败。
-u fd 使用文件描述符 fd 作为输入源,而不是标准输入,类似于重定向。

示例

#!/bin/bash

#第一次输入密码
read -t 20 -sp "Enter password in 20 seconds(once) > " pass1 && printf "
" &&
#第二次输入密码
read -t 20 -sp "Enter password in 20 seconds(again)> " pass2 && printf "
" && 
#判断两次输入的密码是否相等
if [[ $pass1 == $pass2 ]];then
    echo "Valid password"
else
    echo "Invalid password"
    exit 1
fi
echo -e -n "33[31m";read -p "input: " TEST;echo -e "33[0m"
#!/bin/bash

#第一次输入密码
echo -e -n "33[34m";read -t 20 -sp "Enter password in 20 seconds(once) > " pass1;echo -e "33[0m"
#第二次输入密码
echo -e -n "33[34m";read -t 20 -sp "Enter password in 20 seconds(again)> " pass2;echo -e "33[0m"
#判断两次输入的密码是否相等
if [[ $pass1 == $pass2 ]];then
    echo "Valid password"
else
    echo "Invalid password"
    exit 1
fi
原文地址:https://www.cnblogs.com/iuskye/p/shell-read.html