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. ubuntu kylin 14.04安装配置MongoDB v2.6.1(转)

    1.获取最新版本 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz 2.解压并进入bin目录 tar zxvf mongo ...

  2. 项目自动化建构工具gradle 入门2——log4j输出helloWorld

    上一章节呢,有一个能跑的程序了.但是对做工程的人来说,用日志输出感觉比用System.out要有档次一点.比如使用log4j.直接上例子: 1进入D:\work\gradle\log目录  ,您电脑没 ...

  3. ultraiso制作大于4GB的镜像的启动盘

    ultraiso这个软件用来做启动盘很方便, 一般linux啦, windows啦, 神马的都用他来做, 但是, 因为ubuntu一般只有1-2GB, win桌面版一般也就3GB左右, 所以不必考虑这 ...

  4. (一)java arcgis开发环境搭建

    一,整个开发环境 OS:Win7 Development: eclipse 4.3.2 框架:spring+springMVC+mybatis+jquery Arcgis版本:10.2 desktop ...

  5. 解决 node-gyp rebuild 卡住 的问题

    node-gyp在编译前会首先尝试下载node的headers文件,像这样: gyp http GET https://nodejs.org/download/release/v6.8.1/node- ...

  6. ZooKeeper 笔记(6) 分布式锁

    目前分布式锁,比较成熟.主流的方案有基于redis及基于zookeeper的二种方案. 大体来讲,基于redis的分布式锁核心指令为SETNX,即如果目标key存在,写入缓存失败返回0,反之如果目标k ...

  7. [LeetCode] Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  8. 基于C/S架构的3D对战网络游戏C++框架 _04客户端详细设计与OpenGL、Qt基础

    本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...

  9. ie6、7、8兼容部分css3

    做法很简单下载一个ie-css3,然后有css3的样式里加入behavior: url(js/ie-css3.htc)就可以了(js/ie-css3.htc是文件地址) <!DOCTYPE ht ...

  10. 一个简单oop的changeTab

    好多地方都会用到这样一个效果“点击tab切换内容页”,根据自己的想法实现了一下,写了个简单的插件.以前写代码都是标准的函数式编程,现在觉得面向对象编程看起来比较爽,并且更容易维护,于是就用oop的思想 ...