每天写点shell——read的用法
1、read基本读取
#!/bin/bash
#testing the read command echo -n "Enter you name:" #echo -n 让用户直接在后面输入
read name #输入的多个文本将保存在一个变量中
echo "Hello $name, welcome to my program."
执行:
# ./read.sh
Enter you name: wangtao
Hello wangtao, welcome to my program.
2、read -p (直接在read命令行指定提示符)
#!/bin/bash
#testing the read -p option
read -p "Please enter your age: " age
days=$[ $age * ]
echo "That makes you over $days days old!"
执行:
# ./age.sh
Please enter your age:
That makes you over days old!
3、read -p (指定多个变量)
#!/bin/bash
# entering multiple variables read -p "Enter your name:" first last
echo "Checking data for $last, $first"
执行:
# ./read1.sh
Enter your name: a b
Checking data for b, a
4、read 命令中不指定变量,那么read命名将它收到的任何数据都放在特殊环境变量REPLY中
#!/bin/bash
# testing the REPLY environment variable read -p "Enter a number: "
factorial=1
for (( count=; count<= $REPLY; count++ ))
do
factorial=$[ $factorial * $count ] #等号两端不要有空格
done
echo "The factorial of $REPLY is $factorial"
执行:
./read2.sh
Enter a number:
The factorial of is
5、超时, 等待输入的秒数(read -t)
#!/bin/bash
# timing the data entry if read -t -p "Please enter your name: " name #记得加-p参数, 直接在read命令行指定提示符
then
echo "Hello $name, welcome to my script"
else
echo
echo "Sorry, too slow!"
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命令对输入的字符判断
#!/bin/bash
# getting just one character of input read -n1 -p "Do you want to continue [Y/N]? " answer
case $answer in
Y | y) echo
echo "fine, continue on...";;
N | n) echo
echo "OK, goodbye"
exit;;
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)
#!/bin/bash
# hiding input data from the monitor read -s -p "Enter your passwd: " pass #-s 参数使得read读入的字符隐藏
echo
echo "Is your passwd readlly $pass?"
~
执行:
# ./read5.sh
Enter your passwd:
Is your passwd readlly osfile@?
7、从文本中读取
#!/bin/bash
# reading data from a file count=
cat test | while read line
do
echo "Line $count: $line"
count=$[ $count + ]
done
echo "Finished processing the file"
执行:
./read6.sh
Line : The quick brown dog jumps over the lazy fox.
Line : This is a test, this is only a test.
Line : O Romeo, Romeo! Wherefore art thou Romeo?
Finished processing the file
每天写点shell——read的用法的更多相关文章
- centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件、目录属性  shell数组简单用法 $( ) 和${ } 和$(( )) 与  sh -n  sh -x  sh -v 第三十五节课
		centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件.目录属性 shell数组简单用法 $( ) 和$ ... 
- 写个shell脚本
		以前更新网站程序都是手动噼里啪啦敲代码,即麻烦又慢,还神经紧张.终于忍不住写个shell脚本. cd /usr/local/tomcat7/apache-tomcat-9.0.0.M4/ bin/ ... 
- Shell expr的用法  bc 命令   let命令
		Shell expr的用法 bc 命令 let命令 数学运算 let命令 expr命令 bc命令 $(()) $[] http://www.80ops.cn/archives/245. ... 
- hbase基本概念和hbase shell常用命令用法
		1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ... 
- 【转载】HBase基本概念和hbase shell常用命令用法
		1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ... 
- window平台写的shell脚步在Linux不识别
		---恢复内容开始--- 出现的问题是 写的shell脚步在Linux执行的时候不被识别 解决方案: 1.确保用户对文件有读写及执行权限 oracle@linux-106:~/RMAN/bin> ... 
- syntax error near unexpected token `do(写的shell脚本出现格式问题)--->1.问题2.展示信息3.解决方案
		1问题:Linux和windows下的回车换行符不兼容的问题 [root@node-01 script]# sh start_zk.sh art_zk.sh: line 3: syntax error ... 
- shell字符串的用法
		shell字符串的用法 注意:shell4.2和shell4.1会有差别,较低版本的shell可能不支持某些功能 获取字符串长度:${#string} 获取子串: 注:(左边的第一个字符是用 0 表示 ... 
- 好记性不如烂笔头--linux学习笔记9练手写个shell脚本
		#!/bin/bash #auto make install httpd #by authors baker95935 #httpd define path variable H_FILES=http ... 
随机推荐
- 简单socket()编程
			客户端: 1.socket( int af, int type, int protocol) socket()函数用于根据指定的地址族.数据类型和协议来分配一个套接口的描述字及其所用的资源.如果协议p ... 
- QC在win7下不能访问QC服务器介绍
			本地访问不了服务器QC的主要几个原因总结 服务器serverjbossextensionhpcmd 2016-03-24 兼容性问题: 1.在服务端QC的安装目录下jboss\server\def ... 
- [LeetCode] Ternary Expression Parser 三元表达式解析器
			Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ... 
- MVC跨域CORS扩展
			一般的基于浏览器跨域的主要解决方法有这么几种:1.JSONP 2.IFrame方式 3.通过flash实现 4.CORS跨域资源共享 ,这里我们主要关注的是在MVC里面的CORS ... 
- Linux下双网卡绑定bond0
			一:原理: linux操作系统下双网卡绑定有七种模式.现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多.而一般企业都会使用linux操作系统下自带的网卡绑 ... 
- Python文件读写
			读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的 文件打开模式 模式 意义 r 文本只读 w 文本只写 rb 二进制读 rw 二进制写 打开文件 选择一个模式打开一个文件 ... 
- 关于SQL注入和如何防止
			之前在笔试的时候没有很好的答出这个问题,因此我要总结一下问题,以免日后继续在这个地方跌倒,以下是自己的理解,如有错误请指出 一.什么是SQL注入 SQL注入就是服务器在根据业务去处理数据库的时候,客户 ... 
- 【BZOJ 3445】【Usaco2014 Feb】Roadblock
			http://www.lydsy.com/JudgeOnline/problem.php?id=3445 加倍的边一定在最短路上(否则继续走最短路). 最短路长度是O(n)的,暴力扫最短路上的每条边, ... 
- 用上了ReSharper
			用上了ReSharper,发现很爽1.使用var2.去掉无用的using3.将foreach改为linq let4.字符串为cost5.作用域与变量名适配6.文件夹与namespace不匹配7.去掉为 ... 
- CSS清除浮动float方法总结
			使用浮动造成的BUG: 使用浮动前:(子节点是将父节点撑开了) 代码如下 <div class="box"> <div class="d1"& ... 
