read 命令详解

read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量

语法选项

-p  read –p “提示语句”,则屏幕就会输出提示语句,等待输入,并将输入存储在REPLY中
-n  read –n 个数
-t  read –t 时间
-s  read -s 选项能够使read命令中输入的数据不显示在监视器上

例子

  1. 从标准输入读取输入并赋值给变量
[xiess@layzj022301 ~]$ read readfile
hello,world!!welcome to shell
[xiess@layzj022301 ~]$ echo $readfile
hello,world!!welcome to shell
[xiess@layzj022301 ~]$
  1. 等待一组输入,每个单词之间使用空格隔开,直到回车结束,并分别将单词依次赋值给这三个读入变量。
[xiess@layzj022301 ~]$ read frist second third
the_one the_two the_three
[xiess@layzj022301 ~]$ echo "$frist" "$second" "$third"
the_one the_two the_three
[xiess@layzj022301 ~]$
  1. REPLY示例
[xiess@layzj022301 ~]$ read -p "Enter your name: "
Enter your name: admin_xiess
[xiess@layzj022301 ~]$ echo $REPLY
admin_xiess
[xiess@layzj022301 ~]$
  1. 关闭显示
[root@cinder01 ~]# cat readfile.sh 
#!/bin/bash
read -s -p "Enter your password: " password
echo
echo "your password is $password"
[root@cinder01 ~]# chmod a+x readfile.sh 
[root@cinder01 ~]# sh readfile.sh 
Enter your password: 
your password is 123456
原文地址:https://www.cnblogs.com/xieshengsen/p/6666972.html