1、read基本读取

#!/bin/bash
#testing the read command echo -n "Enter you name:" #echo -n 让用户直接在后面输入
read name #输入的多个文本将保存在一个变量中
echo "Hello $name, welcome to my progra

执行:

# ./read.sh
Enter you name: yuan
Hello yuan, welcome to my program  

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

#!/bin/bash
#testing the read -p option
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That makes you over $days 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 -t)

#!/bin/bash
# timing the data entry if read -t 5 -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命令对于输入字符的判断

  • []有比较的判断的功能
  • -o代表or
  • &&在shell中的用法是如果&&左边的命令执行成功(即$?=0)时才能执行&&右边的命令
#!/bin/bash  

read -p "Please input(Y/N):" yn
[ "$yn" == "Y" -o "$yn" == "y" ]&&echo "OK,continue"&&exit 0
[ "$yn" == "N" -o "$yn" == "n" ]&&echo "Oh,interrupt!"&&exit 0
echo "i don't konw what your choice is"&&exit 0

 执行:

yuanqiangfei@ubuntu:~/script$ ./sh01.sh
Please input(Y/N):y
OK,continue
yuanqiangfei@ubuntu:~/script$ ./sh01.sh
Please input(Y/N):n
Oh,interrupt!

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

  • 在中括号 [] 内的每个组件都需要有空白键来分隔;
  • 在中括号内的变量,最好都以双引号括号起来;
  • 在中括号内的常数,最好都以单或双引号括号起来。
  • ==和!=两边都要有空格
  • []有比较的判断的功能
#!/bin/bash
#entering muiltple variables while true
do
read -s -p "Please enter your password:" passwd [ "$passwd" == "123456" ]&&echo "password is right!"&&exit 0
[ "$passwd" != "123456" ]&&echo "password is not right,Please input again!"&&continue
done

执行:

yuanqiangfei@ubuntu:~/script$ ./read.sh
Please enter your password:password is right!
yuanqiangfei@ubuntu:~/script$ ./read.sh
Please enter your password:password is not right,Please input again!
Please enter your password:

7、从文本中读取

#!/bin/bash
# reading data from a file count=1
cat test | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done
echo "Finished processing the file"

执行结果:

 ./read6.sh
Line 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

  

shell script中read的用法的更多相关文章

  1. Shell编程中Shift的用法

    Shell编程中Shift的用法 位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shif ...

  2. shell script中的$*和$@

    在shell script中,$*和$@都是获取所有的命令行参数,但是这两者在使用的过程中会有细微的差别,差别主要是在有没有使用双引号,即是直接使用$*,$@,还是使用"$*",& ...

  3. Linux shell脚本中shift的用法说明【转】

    本文转载自:http://blog.csdn.net/zhu_xun/article/details/24796235 Linux shell脚本中shift的用法说明 shift命令用于对参数的移动 ...

  4. Shell编程中Shift的用法【转】

    本文转载自:http://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html Shell编程中Shift的用法 位置参数可以用shift ...

  5. 详解shell编程中2>&1用法

    在使用 linux 命令或者 shell 编程时,这个用法常会遇到 2>&1 下面看一个命令示例,然后分析下他是如何工作的: ls foo > /dev/null 2>&am ...

  6. shell脚本中case的用法

    shell脚本中case选择语句可以结合read指令实现比较好的交互应答操作,case接收到read指令传入的一个或多个参数,然后case根据参数做选择操作. case的语法如下 case $char ...

  7. Pig 在 shell script中被调用,批量载入处理文件

    首先,我想达到的目的是批量的处理一个目录下的的很多文档,这些文档保存了我要处理的数据,由于pig是初学..所以不知到该怎么批量的load,没有写过 自己的UDF,仅仅能一个一个文件的load,然后处理 ...

  8. shell脚本中${...}函数的用法总结

    ${...}在字符串中有非常多的用法: 1.${var} 限定变量. 如果一个变量名A是另一个变量名AB的前部分,那么,如果要在AB相连时,要得到A的值,就必须使用${var}限定. 如果变量名不会产 ...

  9. 在shell script中进行数值运算的两种方法

    方法1:使用"$((计算式))"的方式进行数值运算,不需要使用declare命令显示声明数值型变量来存储计算结果: 方法2:使用declare命令配合"-i"选 ...

随机推荐

  1. suoi46 最大和和 (线段树)

    <Segment tree Beats!>,反正我不会 #include<bits/stdc++.h> #define pa pair<int,int> #defi ...

  2. CF1114B Yet Another Array Partitioning Task(贪心,构造题)

    我至今不敢相信我被这么一道简单的题卡了这么久……看来还是太弱了…… 题目链接:CF原网 题目大意:定义一个序列的“美丽度”为这个序列前 $m$ 大的数的和.现在有一个长度为 $n$ 的序列,你需要把它 ...

  3. 提高磁盘访问性能 - NtfsDisableLastAccessUpdate

    这个技巧可以提高磁盘访问性能,不过仅适用于NTFS文件系统. 我们知道,当在磁盘管理应用程序中列出目录结构时──效果类似“资源管理器”.“文件管理 器”(Windows NT  3.xx/4.0下的称 ...

  4. Flash10下复制到剪切板的一种新方法

    web开发中常常要实现“复制到剪切板”功能.这个功能很实用,但是由于安全问题,浏览器的限制越来越严,实现的方法也越来越有限了.Firefox默认下不能直接通过Javascript操作剪切板,必须开启相 ...

  5. 弹窗中修改select默认值遇到的问题

    弹窗中修改select默认值无效 前提: 项目中遇到一个需求, 在一个弹窗中,有很多个select都是在打开弹窗的同时由js自动生成的(每次打开都自动重新生成一遍). 弹窗打开后,从每个select的 ...

  6. NO.8:绝不在构造或者析构过程中调用virtual函数

    在构造和析构执行期间不要调用virtual函数,因为这类调用从不会下降至derived class(比起当前执行构造函数和析构函数) 如果在base class 构造函数或者析构函数调用virtual ...

  7. PHP--traits

    从PHP5.4.0起,PHP实现了代码复用的一个方法,称为traits. traits试图用来解决像PHP这样的面向对象语言中只支持单继承的问题.它的作用是允许我们在不使用继承的情况下为一个类增加功能 ...

  8. table中表头不动,表体产生滚动条

    <div id="elec_table"> 2 <div class="table-head"> 3 <table> 4 & ...

  9. Linux 4.10.8 根文件系统制作(三)---制作yaffs文件系统

    这里直接用的是韦东山提供的工具. yaffs文件系统是专门用于nandflash的文件系统 我们前面已经下载了yaffs 的源码,也做了文件系统目录的构建. 在yaffs2的源码目录中有一个utils ...

  10. "ls: cannot access sys/class/ieee80211: No such file or directory" .

    1- Do update and upgrade as always. apt-get update && apt-get upgrade && apt-get dis ...