shell之read用法
转自:https://www.cnblogs.com/wangtao1993/archive/2016/12/06/6136169.html
-p | 指定要显示的提示 |
-s | 静默输入,一般用于密码 |
-n # | 指定输入的字符长度最大值# |
-d ‘字符’ | 输入结束符,当你输入的内容出现这个字符时,立即结束输入 |
-t N | 超出N秒没有进行输入,则自动退出。 |
1、read基本读取
1 #!/bin/bash2 #testing the read command3 4 echo -n "Enter you name:" #echo -n 让用户直接在后面输入 5 read name #输入的多个文本将保存在一个变量中6 echo "Hello $name, welcome to my program."
执行:
# ./read.sh Enter you name: wangtao Hello wangtao, welcome to my program.
2、read -p (直接在read命令行指定提示符)
1 #!/bin/bash2 #testing the read -p option3 read -p "Please enter your age: " age4 days=$[ $age * 365 ]5 echo "That makes you over $days days old!"
执行:
# ./age.sh Please enter your age: 23 That makes you over 8395 days old!
3、read (指定多个变量)
1 #!/bin/bash2 # entering multiple variables3 4 read -p "Enter your name:" first last5 echo "Checking data for $last, $first"
执行:
# ./read1.sh Enter your name: a b Checking data for b, a
4、read 命令中不指定变量,那么read命名将它收到的任何数据都放在特殊环境变量REPLY中
1 #!/bin/bash2 # testing the REPLY environment variable3 4 read -p "Enter a number: "5 factorial=1 6 for (( count=1; count<= $REPLY; count++ ))7 do8 factorial=$[ $factorial * $count ] #等号两端不要有空格9 done10 echo "The factorial of $REPLY is $factorial"
执行:
./read2.sh Enter a number: 6 The factorial of 6 is 720
5、超时, 等待输入的秒数(read -t)
1 #!/bin/bash2 # timing the data entry3 4 if read -t 5 -p "Please enter your name: " name #记得加-p参数, 直接在read命令行指定提示符5 then6 echo "Hello $name, welcome to my script"7 else8 echo 9 echo "Sorry, too slow!"10 fi
执行:
# ./read3.sh Please enter your name: Sorry, too slow!
# ./read3.sh Please enter your name: wang Hello wang, welcome to my script
5、read命令对输入的字符判断
1 #!/bin/bash2 # getting just one character of input3 4 read -n1 -p "Do you want to continue [Y/N]? " answer5 case $answer in6 Y | y) echo7 echo "fine, continue on...";;8 N | n) echo 9 echo "OK, goodbye"10 exit;;11 esac
执行:
# ./read4.sh Do you want to continue [Y/N]? y fine, continue on..../read4.sh Do you want to continue [Y/N]? n OK, goodbye
6、隐藏方式读取(read -s)
1 #!/bin/bash2 # hiding input data from the monitor3 4 read -s -p "Enter your passwd: " pass #-s 参数使得read读入的字符隐藏5 echo 6 echo "Is your passwd readlly $pass?" ~
执行:
# ./read5.sh Enter your passwd: Is your passwd readlly osfile@206?
7、从文本中读取
1 #!/bin/bash2 # reading data from a file3 4 count=15 cat test | while read line6 do7 echo "Line $count: $line"8 count=$[ $count + 1 ]9 done10 echo "Finished processing the file"
执行:
./read6.sh Line 1: The quick brown dog jumps over the lazy fox. Line 2: This is a test, this is only a test. Line 3: O Romeo, Romeo! Wherefore art thou Romeo? Finished processing the file
shell之read用法相关推荐
- 01 HBase基本概念和hbase shell常用命令用法
本文转载自:http://archive.cnblogs.com/a/2178064/ 1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一 ...
- HBase基本概念和hbase shell常用命令用法
1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...
- shell实例第20讲:linux shell date的用法
linux shell date的用法 转自:https://www.cnblogs.com/faberbeta/p/linux-shell002.html 1.date中的参数 %% 一个文字的 % ...
- SHELL test [ 命令用法
From: http://blog.csdn.net/ubuntulover/article/details/6978305 原文地址:http://www.examw.com/linux/all/1 ...
- shell字符串的用法
shell字符串的用法 注意:shell4.2和shell4.1会有差别,较低版本的shell可能不支持某些功能 获取字符串长度:${#string} 获取子串: 注:(左边的第一个字符是用 0 表示 ...
- Shell expr的用法 bc 命令 let命令
Shell expr的用法 bc 命令 let命令 数学运算 let命令 expr命令 bc命令 $(()) $[] http://www.80ops.cn/archives/245. ...
- Shell的基础用法
Shell的基础用法 一.shell概述 shell是一个命令行解释器,它接受应用程序/用户命令.然后调用操作系统内核. shell也是一个功能强大的编程语言,易编写.易调试.灵活性高. 1)Linu ...
- Linux Shell nohup命令用法(内含代码演示)
一.Linux Shell nohup命令用法 在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: ...
- linux until工具,Linux shell之until用法
Linux shell之until用法 #!/bin/bash #until用法,显示变量值从0到99 varl=0 #定义变量 echo & ...
- shell脚本—case用法
shell脚本-case用法 文章目录 shell脚本-case用法 1.什么是case? 2.case使用场景 3.case语法 1.什么是case? case语句和if类似,也是用来判断的,只不过 ...
最新文章
- Servlet - HTTP超文本传输协议
- Google C++单元测试框架GoogleTest---AdvancedGuide(译文)上
- css --- 伸缩布局,让图片居中
- js的深浅拷贝( 赋值后原值被覆盖的问题 )
- Linux下的信号处理
- 使用 PlantUML 绘制时序图
- [学习笔记]--ASP.Net MVC
- C处理Python返回的字串代码
- ecshop mysql密码忘记_ECSHOP后台密码忘记了怎么办
- PC微信多开,超简单
- 忘记了PPT文件打开密码怎么办?
- SQLSERVER2008 18456错误
- 硬件工程师七夕鹊桥设计锦集
- 低配本用win10服务器系统,低配电脑用win7还是win10比较好_低配置电脑装win7还是win10系统合适...
- STM8S---IO复用配置(STVP方式)
- locust之安装(3)
- git 合并分支(开发分支dev合并到主分支master)
- springboot通过ITextPDF写入模板并下载
- 谷歌浏览器报错ERR_MANDATORY_PROXY_CONFIGURATION_FAILED 解决办法
- Appium实现app自动化测试