加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Shell中read的用法详解

发布时间:2020-12-15 16:29:21 所属栏目:安全 来源:网络整理
导读:read的常用用法如下: read -[pstnd] var1 var2 ... -p 提示语句 -n 字符个数 -s 屏蔽回显 -t 等待时间 -d 输入分界 01). read # 从标准输入读取一行并赋值给特定变量REPLYroot@linux~# readHello,World!root@linux~# echo $REPLYHello,World!02). read name

read的常用用法如下:

read -[pstnd] var1 var2 ...

-p提示语句
-n 字符个数
-s 屏蔽回显
-t 等待时间
-d 输入分界

01). read      						# 从标准输入读取一行并赋值给特定变量REPLY
root@linux~# read
Hello,World!
root@linux~# echo $REPLY
Hello,World!

02). read name 						# 从标准输入读取输入并赋值给变量name
root@linux~# read name
Jerry
root@linux~# echo $name
Jerry

03). read var1 var2   				# 第一个变量放置于var1,第二个变量放到var2  
root@linux~# read firstname lastname
Jerry Gao
root@linux~# echo "firstname:$firstname lastname:$lastname"
firstname:Jerry lastname:Gao

04). read -p "text"   				# 打印提示'text',等待输入,并将输入存储在REPLY中
root@linux~# read -p 'Please Enter your name:-->'
Please Enter your name:-->Jerry
root@linux~# echo $REPLY
Jerry

05). read -p "text" var  			# 打印提示'text',等待输入,并将输入存储在VAR中
root@linux~# read -p 'Please Enter your name:-->' name
Please Enter your name:-->Jerry
root@linux~# echo $name
Jerry

06). read -p "text" var1  var2		# 打印提示'text',等待输入,将变量分别存储在var1,var2...
root@linux~# read -p 'What your name? ' firstname lastname
What your name? Jerry Gao
root@linux~# echo "Firstname:$firstname Lastname:$lastname"
Firstname: Jerry Lastname:Gao

07). read -r line 					# 允许输入包含反斜杠
root@linux~# read line          	# 不带-r参数;则反斜杠不显示
This is line 1.  This is line 2.
root@linux~# echo $line
This is line 1. This is line 2.

root@linux~# read -r line       	# 带-r参数;则反斜杠正常显示显示
This is line 1.  This is line 2.
root@linux~# echo $line
This is line 1.  This is line 2.

08). read -t 5						# 指定读取等待时间为5秒
root@linux~# read -t 5 -p 'Your Name:' name
Your Name:Jerry
root@linux~# echo $name       		# 如果5秒还未输入,则不能输入
Jerry

09). read -a arrayname         		# 把单词清单读入arrayname的数组里
root@linux~# read -a citys
BJ SH CD GZ
root@linux~# echo ${citys[*]}
BJ SH CD GZ
root@linux~# echo ${citys[0]}
BJ

10). read -s -p "pwd:" pwd 		# 使用-s参数可以不显示用户的输入
root@linux~# read -p "Enter Your Password:" -s PASSWORD
Enter Your Password:
root@linux~#
root@linux~# echo $PASSWORD			# 刚才输入的密码为:1234
1234


11). read -n 1 -p "Sure?(y/n):"  	# 使用-n,来确定参数个数
root@linux~# read -n 1 -p "Are you sure?(y/n): " ANSWER
Are you sure?(y/n): y              
root@linux~#
root@linux~# echo -e "Your ANSWER is: $ANSWER"
Your ANSWER is: y

12). read -d ":" var				# 使用:作为输入分界符
root@linux~# read -d ";" -p "Enter Your Name:" name
Enter Your Name:Jerry Gao;
root@linux~# echo -e "Your Name: $name"
Your Name: Jerry Gao


read在脚本中的应用:

遍历方式一:

#!/bin/bash

count=0

while read line
do
    echo -e "$count:-->$line"
    count=$[ count + 1]
done < /etc/passwd

遍历方式二:

#!/bin/bash

awk -F: '{print $1,$7}' /etc/passwd | while read user bash
do
    echo -e "USER=$user; BASH=$bash"
done

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读