博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shell编程,read的用法
阅读量:7133 次
发布时间:2019-06-28

本文共 2729 字,大约阅读时间需要 9 分钟。

hot3.png

导读 本文介绍Shell编程中read的用法

1、read基本读取

1 #!/bin/bash  2 #testing the read command  3   4 echo -n "Enter you name:"   #echo -n 让用户直接在后面输入   5 read name  #输入的多个文本将保存在一个变量中  6 echo "Hello $name, welcome to my program."                                      执行:# ./read.shEnter you name: wangtaoHello wangtao, welcome to my program.

2、read -p (直接在read命令行指定提示符)

1 #!/bin/bash  2 #testing the read -p option  3 read -p "Please enter your age: " age  4 days=$[ $age * 365 ]  5 echo "That makes you over $days days old!"执行:# ./age.shPlease enter your age: 23That makes you over 8395 days old!

3、read -p (指定多个变量)

1 #!/bin/bash  2 # entering multiple variables  3   4 read -p "Enter your name:" first last  5 echo "Checking data for $last, $first"执行:# ./read1.shEnter your name: a bChecking data for b, a

4、read 命令中不指定变量,那么read命名将它收到的任何数据都放在特殊环境变量REPLY中

1 #!/bin/bash  2 # testing the REPLY environment variable  3   4 read -p "Enter a number: "  5 factorial=1                           6 for (( count=1; count< = $REPLY; count++ ))  7 do  8    factorial=$[ $factorial * $count ]   #等号两端不要有空格  9 done 10 echo "The factorial of $REPLY is $factorial"执行:./read2.shEnter a number: 6The factorial of 6 is 720

5、超时, 等待输入的秒数(read -t)

1 #!/bin/bash  2 # timing the data entry  3   4 if read -t 5 -p "Please enter your name: " name     #记得加-p参数, 直接在read命令行指定提示符  5 then  6     echo "Hello $name, welcome to my script"  7 else  8     echo   9     echo "Sorry, too slow!" 10 fi执行:# ./read3.shPlease enter your name: Sorry, too slow!# ./read3.sh Please enter your name: wangHello wang, welcome to my script

6、read命令对输入的字符判断

1 #!/bin/bash  2 # getting just one character of input  3   4 read -n1 -p "Do you want to continue [Y/N]? " answer  5 case $answer in  6 Y | y) echo  7        echo "fine, continue on...";;  8 N | n) echo   9        echo "OK, goodbye" 10        exit;; 11 esac   执行:# ./read4.shDo you want to continue [Y/N]? yfine, continue on..../read4.shDo you want to continue [Y/N]? nOK, goodbye

7、隐藏方式读取(read -s)

1 #!/bin/bash  2 # hiding input data from the monitor  3   4 read -s -p "Enter your passwd: " pass   #-s 参数使得read读入的字符隐藏  5 echo   6 echo "Is your passwd readlly $pass?"~                                          执行:# ./read5.shEnter your passwd: Is your passwd readlly osfile@206?

8、从文本中读取

1 #!/bin/bash  2 # reading data from a file  3   4 count=1  5 cat test | while read line  6 do  7    echo "Line $count: $line"  8    count=$[ $count + 1 ]  9 done 10 echo "Finished processing the file"执行: ./read6.shLine 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

原文来自: 

转载于:https://my.oschina.net/ssdlinux/blog/3006452

你可能感兴趣的文章
Computer Vision & MultiMedia 相关国际会议汇总
查看>>
vs2008在win7系统中安装不问题
查看>>
HDU-1520 Anniversary party
查看>>
springmvc web.xml配置之 -- ContextLoaderListener
查看>>
java_数组作缓存池的不可变类实例
查看>>
webservice主流框架Axis、Axis2、XFire、CXF的比较
查看>>
lambda
查看>>
Master Nginx(3) - Using the Mail Module
查看>>
4、jeecg 笔记之 自定义显示按钮 (exp 属性)
查看>>
大白话5分钟带你走进人工智能-第二十八节集成学习之随机森林概念介绍(1)
查看>>
ASPNET MVC Error 403.14
查看>>
redis学习笔记
查看>>
排球计分规则
查看>>
xml解析
查看>>
android分析之Condition
查看>>
创建单例的两种方法
查看>>
Mac上在github 搭建 octopress
查看>>
Elasticsearch增、删、改、查操作深入详解
查看>>
Cocos2d-x内存自动释放机制--透彻篇
查看>>
模板方法模式
查看>>