shell script中read的用法
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的用法的更多相关文章
- Shell编程中Shift的用法
Shell编程中Shift的用法 位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shif ...
- shell script中的$*和$@
在shell script中,$*和$@都是获取所有的命令行参数,但是这两者在使用的过程中会有细微的差别,差别主要是在有没有使用双引号,即是直接使用$*,$@,还是使用"$*",& ...
- Linux shell脚本中shift的用法说明【转】
本文转载自:http://blog.csdn.net/zhu_xun/article/details/24796235 Linux shell脚本中shift的用法说明 shift命令用于对参数的移动 ...
- Shell编程中Shift的用法【转】
本文转载自:http://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html Shell编程中Shift的用法 位置参数可以用shift ...
- 详解shell编程中2>&1用法
在使用 linux 命令或者 shell 编程时,这个用法常会遇到 2>&1 下面看一个命令示例,然后分析下他是如何工作的: ls foo > /dev/null 2>&am ...
- shell脚本中case的用法
shell脚本中case选择语句可以结合read指令实现比较好的交互应答操作,case接收到read指令传入的一个或多个参数,然后case根据参数做选择操作. case的语法如下 case $char ...
- Pig 在 shell script中被调用,批量载入处理文件
首先,我想达到的目的是批量的处理一个目录下的的很多文档,这些文档保存了我要处理的数据,由于pig是初学..所以不知到该怎么批量的load,没有写过 自己的UDF,仅仅能一个一个文件的load,然后处理 ...
- shell脚本中${...}函数的用法总结
${...}在字符串中有非常多的用法: 1.${var} 限定变量. 如果一个变量名A是另一个变量名AB的前部分,那么,如果要在AB相连时,要得到A的值,就必须使用${var}限定. 如果变量名不会产 ...
- 在shell script中进行数值运算的两种方法
方法1:使用"$((计算式))"的方式进行数值运算,不需要使用declare命令显示声明数值型变量来存储计算结果: 方法2:使用declare命令配合"-i"选 ...
随机推荐
- SharePoint 2013 pre-config
#region 1. add winrm, application server and web server role Add-WindowsFeature winrm-iis-ext, Appli ...
- json序列化 & 反序列化
json序列化: json的dumps方法可以将json格式数据序列为python的相关数据类型,比如str,常用于打印,另外,在序列化时,中文汉字被转换为unicode编码,在dumps函数中添加参 ...
- Codeforces 382 D Permutations
题目大意: 给出一张二分图,这张二分图完美匹配的个数是奇数,求删掉第$i(1<=i<=m)$条边后完美匹配个数的奇偶性. 设这张图的邻接矩阵为$A$,那么完美匹配的个数为$A$的积和式,即 ...
- selectionStart和selectionEnd属性
在网上看了很多用js在光标处插入文字的代码,很多都没有用,互抄而已,浪费时间.最近用到一个新方法,可以到达这个需求.IE支持document.selection,Firefox,Chrome,Safa ...
- 解决IIS8中 URLRewriter 不能使用的方法
1.把应用程序池改成集成 2.的web.config 加入代码 <system.webServer> <validation validateIntegratedModeConfig ...
- springboot项目中jdk版本的问题
几经周折,在idea中修改了多次jdk编译为1.8,可是一编译就恢复到默认的1.7版本. 在经过多次这个博客的修改内容: https://blog.csdn.net/li396864285/artic ...
- (母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- JavaSE学习总结(三)——Java语言编程练习、格式化字符与常量
一.变量.常量.字面量 package com.zhangguo.chapter2_3; /** * 1.银行利率为5%,问存款100美元5年的收益细节? * */ public class P1 { ...
- Lucene Query In Kibana
1. Terms 一个查询由词条与操作组成.词条可以是单词,或者短语. hello #单独项 "hello pzdn" #双引号引起来短语 2. Field Lucene 支持字段 ...
- I2C和SPI总线对比
1 iic总线不是全双工,2根线SCL SDA.spi总线实现全双工,4根线SCK CS MOSI MISO 2 iic总线是多主机总线,通过SDA上的地址信息来锁定从设备.spi总线只有一个主设备, ...