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的用法的更多相关文章

  1. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  2. 写个shell脚本

    以前更新网站程序都是手动噼里啪啦敲代码,即麻烦又慢,还神经紧张.终于忍不住写个shell脚本.   cd /usr/local/tomcat7/apache-tomcat-9.0.0.M4/ bin/ ...

  3. Shell expr的用法 bc 命令 let命令

    Shell expr的用法  bc 命令   let命令 数学运算 let命令  expr命令  bc命令  $(())   $[] http://www.80ops.cn/archives/245. ...

  4. hbase基本概念和hbase shell常用命令用法

    1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...

  5. 【转载】HBase基本概念和hbase shell常用命令用法

    1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...

  6. window平台写的shell脚步在Linux不识别

    ---恢复内容开始--- 出现的问题是 写的shell脚步在Linux执行的时候不被识别 解决方案: 1.确保用户对文件有读写及执行权限 oracle@linux-106:~/RMAN/bin> ...

  7. 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 ...

  8. shell字符串的用法

    shell字符串的用法 注意:shell4.2和shell4.1会有差别,较低版本的shell可能不支持某些功能 获取字符串长度:${#string} 获取子串: 注:(左边的第一个字符是用 0 表示 ...

  9. 好记性不如烂笔头--linux学习笔记9练手写个shell脚本

    #!/bin/bash #auto make install httpd #by authors baker95935 #httpd define path variable H_FILES=http ...

随机推荐

  1. jq focus 在火狐(Firefox)下无效

    今天写代码的时候发现,在blur事件里面写focus获取焦点无效, $input.blur(function(){ ……………… $input.focus(): } 而且只是在火狐下面无效而已,很明显 ...

  2. redis.conf配置详细解析

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  3. 特殊文件: /dev/null和/dev/tty

    转自:http://www.cnblogs.com/stephen-liu74/archive/2011/11/10/2240461.html Linux系统提供了两个对Shell编程非常有用的特殊文 ...

  4. IBM Bluemix体验:Containers

    国际版的Bluemix目前有三个region,US South,United Kingdom和Sydney.其中US South是功能最全的,UK其次,Sydney功能最少.Containers服务在 ...

  5. JavaScript代码段整理笔记系列(一)

    30段JavaScript代码——上篇 1.如何区分IE及非IE浏览器: if(!+[1,]){ //IE 11 不支持 alert("这是 IE 浏览器"): }else{ al ...

  6. mac下搭建redis环境

    一.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有 ...

  7. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  8. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. ElasticSearch第二步-CRUD之Sense

    ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...

  10. Tower是个不错的项目管理开放平台

    简单,易用,轻量级,挺多大项目都在用. 目前公司的项目也在使用,但是从高层到底下,随意惯了,最终没有用起来. 感觉适合年轻激情的创业公司团队来使用. 附上地址:https://tower.im/