读取键盘输入

read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说)。得到输入后,read命令将数据放入一个标准变量中。

常用的参数:

参数作用
-p提示语句
-t等待时间
-s不回显
#脚本
[dps@ccod131 bak]$ cat read.sh 
#!/bin/bash

read -p "please input:" name
echo "$name"
#执行效果
[dps@ccod131 bak]$ bash read.sh 
please input:zhangaj
zhangaj
  • -p 打印提示(please input:);
  • -t 指定读取等待时长;
# 脚本
[dps@ccod131 bak]$ cat read.sh 
#!/bin/bash

read -p "please input:" -s name
echo 
echo "$name"
# 执行效果
[dps@ccod131 bak]$ bash read.sh 
please input:
zhang
  • -s 不回显
#脚本
[dps@ccod131 bak]$ cat read.sh 
#!/bin/bash

read -p "please input:"  name age 
echo 
echo "name:$name,age:$age"

#执行效果
[dps@ccod131 bak]$ vim read.sh
[dps@ccod131 bak]$ bash read.sh 
please input:zhangaj 25 女      

name:zhangaj,age:25 女
  • 输入多个变量时,按顺序读取给read后边的变量,多出来的值全部赋值给最后一个变量;当变量过少时,后边的变量赋值为空
#脚本
[dps@ccod131 bak]$ cat read.sh 
#!/bin/bash

read -p "please input:" 
echo 
echo "$REPLY"
#执行效果
[dps@ccod131 bak]$ bash read.sh 
please input:hello

hello
  • 默认变量:REPLY

参考链接:http://man.linuxde.net/read

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值