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. 解决Native atomics support not found问题

    今天用arm-none-linux-gnueabi交叉编译libmysqclient.so,出现Native atomics support not found问题 进入mysql-connector ...

  2. 4-安装workpress

    安装wordpress 软件下载 https://cn.wordpress.org/releases/ 在mysql数据库中创建wordpress数据库 mysql> create databa ...

  3. Linux工具快速教程

    看到一linux中常用工具使用教程,非常好.猛击下面的地址 github:https://github.com/me115/linuxtools_rst 在线文档:http://linuxtools- ...

  4. 安卓使用SQlite3数据库无法id主键无法自动增加?不是的。

    安卓使用SQlite3数据库无法id主键无法自动增加?不是的. 要这样写:id integer primary key ,要写integer而不是int所以会报错! http://blog.csdn. ...

  5. BZOJ 1927: [Sdoi2010]星际竞速

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 2051  Solved: 1263[Submit][Stat ...

  6. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

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

  7. C#代码中实现两个表(DataTable)的关联查询(JOIN)

    之前通常都是使用SQL直接从数据库中取出表1和表2关联查询后的数据,只需要用一个JOIN就可以了,非常方便.近日遇到一种情况,两个表中的数据已经取到代码中,需要在代码中将这两个表关联起来,并得到它们横 ...

  8. Android中关于cpu/cpuset/schedtune的应用

    Android中关于cpu/cpuset/schedtune的应用都是基于进程优先级的,根据不同优先级划分进程类型.AMS(ActivityManagerService)和PMS(PackageMan ...

  9. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  10. 【java基础系列】一、常用命令行

    常用的DOS命令: dir:列出当前目录下的文件以及文件夹 md:创建目录 rd:删除目录 cd:进入指定目录 cd..:退回到上一级目录 cd\:退回到根目录 del:删除文件 exit:退出dos ...