每天写点shell——read的用法
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的用法的更多相关文章
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件.目录属性 shell数组简单用法 $( ) 和$ ...
- 写个shell脚本
以前更新网站程序都是手动噼里啪啦敲代码,即麻烦又慢,还神经紧张.终于忍不住写个shell脚本. cd /usr/local/tomcat7/apache-tomcat-9.0.0.M4/ bin/ ...
- Shell expr的用法 bc 命令 let命令
Shell expr的用法 bc 命令 let命令 数学运算 let命令 expr命令 bc命令 $(()) $[] http://www.80ops.cn/archives/245. ...
- hbase基本概念和hbase shell常用命令用法
1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...
- 【转载】HBase基本概念和hbase shell常用命令用法
1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...
- window平台写的shell脚步在Linux不识别
---恢复内容开始--- 出现的问题是 写的shell脚步在Linux执行的时候不被识别 解决方案: 1.确保用户对文件有读写及执行权限 oracle@linux-106:~/RMAN/bin> ...
- 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 ...
- shell字符串的用法
shell字符串的用法 注意:shell4.2和shell4.1会有差别,较低版本的shell可能不支持某些功能 获取字符串长度:${#string} 获取子串: 注:(左边的第一个字符是用 0 表示 ...
- 好记性不如烂笔头--linux学习笔记9练手写个shell脚本
#!/bin/bash #auto make install httpd #by authors baker95935 #httpd define path variable H_FILES=http ...
随机推荐
- PL/SQL连接错误:ora-12705:cannot access NLS data files or invalid environment specified
适合自己的解决方法: 排查问题: 1. 你没有安装Oracle Client软件.这是使用PL/SQL Developer的必须条件.安装Oracle Client后再重试.2. 你安装了多个Orac ...
- wamp下Apache配置vhost
1.由于后面虚拟机中需要用到Rewrite所以先编辑Apache的conf目录下的httpd.conf文件.(我的文件位置是:D:\Program Files\wamp\bin\apache\apac ...
- linux下EOF写法梳理
在平时的运维工作中,我们经常会碰到这样一个场景:执行脚本的时候,需要往一个文件里自动输入N行内容.如果是少数的几行内容,还可以用echo追加方式,但如果是很多行,那么单纯用echo追加的方式就显得愚蠢 ...
- Python资源
25本免费的Python电子书 http://python.jobbole.com/29281/ 9本免费的Python编程书 http://python.jobbole.com/765/
- Ubuntu解压缩命令
原文链接:http://www.linuxidc.com/Linux/2012-08/68122.htm ZIP zip可能是目前使用得最多的文档压缩格式.它最大的优点就是在不同的操作系统平台,比如L ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- Euler-Maruyama discretization("欧拉-丸山"数值解法)
欧拉法的来源 在数学和计算机科学中,欧拉方法(Euler method)命名自它的发明者莱昂哈德·欧拉,是一种一阶数值方法,用以对给定初值的常微分方程(即初值问题)求解.它是一种解决常微分方程数值积分 ...
- 百度地图demo
以下代码拷贝成html,直接运行即能看到百度地图 <!DOCTYPE html><html> <head> <meta http-equiv="Co ...
- php代码基础
如何接入新浪api <?php function getWeiboData() { $count = 15; // 参数source后面输入你的授权号 $url = "https:// ...