linux shell获取用户输入
一、获取用户输入
1、基本读取
read命令接收标准输入的输入,或其它文件描述符的输入。得到输入后,read命令将数据输入放入一个标准变量中。
[root@rac2 ~]# cat t8.sh
#!/bin/bash
#testing the read command
echo -n "enter your name:" ---:-n用于允许用户在字符串后面立即输入数据,而不是在下一行输入。
read name
echo "hello $name ,welcome to my program."
[root@rac2 ~]# ./t8.sh
enter your name:zhou
hello zhou ,welcome to my program.
read命令的-p选项,允许在read命令行中直接指定一个提示:
[root@rac2 ~]# cat t9.sh
#!/bin/bash
#testing the read -p option
read -p "please enter your age:" age ---age与前面必须有空格
echo "your age is $age"
[root@rac2 ~]# ./t9.sh
please enter your age:10
your age is 10
2、在read命令中也可以不指定变量,如果不指定变量,那么read命令会将接收到的数据防止在环境变量REPLAY中
[root@rac2 ~]# cat t10.sh
#!/bin/bash
#tesing the replay environment variable
read -p "enter a number:"
factorial=1
for (( count=1; count<=$REPLY; count++ ))
do
factorial=$[ $factorial * $count ]
done
echo "the factorial of $REPLY is $factorial"
[root@rac2 ~]# ./t10.sh
enter a number:5
the factorial of 5 is 120
3、计时
-t选项指定read命令等待输入的秒数。当计时器计时数满时,read命令返回一个非零退出状态
[root@rac2 ~]# cat t11.sh
#!/bin/bash
#timing the data entry
if read -t 5 -p "please enter your name:" name
then
echo "hello $name ,welcome to my script"
else
echo "sorry ,tow slow!"
fi
[root@rac2 ~]# ./t11.sh
please enter your name:zhou
hello zhou ,welcome to my script
[root@rac2 ~]# ./t11.sh
please enter your name:sorry ,tow slow!
4、默读
有时候需要脚本用户进行输入,但不希望输入的数据显示在监视器上,(实际上是显示的只是read命令将文本颜色设置为与背景相同的了)。
[root@rac2 ~]# cat t12.sh
#!/bin/bash
#hiding input data from the monitor
read -s -p "enter your password:" pass
echo "is your password really $pass?"
[root@rac2 ~]# ./t12.sh
enter your password:is your password really 12345?
5、读取文件
每调用一次read命令都会读取文件中的一行文本,当文件中没有可读的行时,read命令将以非零退出状态退出。
[root@rac2 ~]# cat t13.sh
#!/bin/bash
count=1
cat test | while read line
do
echo "line $count:$line"
count=$[$count + 1]
done
[root@rac2 ~]# ./t13.sh
line 1:zhou
line 2:xiao
line 3:zhou
linux shell获取用户输入的更多相关文章
- shell获取用户输入
主题: 再学shell之获取用户输入echo -n(不换行)和read命令-p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 和“读文件”深入学习 1.基本读取read命令接收标准输 ...
- linux shell获取键盘输入
linux shell从键盘获取输入 代码1: #!/bin/bash #提示“Input your choice:”,把用户的输入保存入变量choice_user中read -p "Inp ...
- linux shell获取show slave status方法
linux shell获取show slave status方法<pre>#!/bin/basharray=($(mysql -u数据库账号 -p数据库密码 -e "show s ...
- 使用scanner工具类来获取用户输入的信息
使用scanner工具类来获取用户输入的成绩信息. 步骤:1.导入java.util.Scanner包 2.创建Scanner对象 3.接受并保存用户输入的值 例子:通过用户输入来获取学生成绩 pac ...
- Java用Scanner类获取用户输入
用Java编写程序时,有些数据需要用户输入,这个时候需要调用java提供的Scanner类,这个类在包java.util下,比如求一个矩形的面积,简单的看一下用法: import java.util. ...
- HTML获取用户输入的几种玩法
input标签 input是一个自闭和标签,可以获得用户的输入 form标签 form标签是用来进行表单提交用的,它把用户的输入内容提交到服务器. 一个注册页面的例子 <!DOCTYPE htm ...
- python学习:注释、获取用户输入、字符串拼接、运算符、表达式
注释 #为单行注释'''三个单引号(或者"""三个双引号)为多行注释,例如'''被注释的内容''' '''三个单引号还可以起到多行打印的功能. #ctrl+? 选中的多行 ...
- 微信小程序-form表单-获取用户输入文本框的值
微信小程序-form表单-获取用户输入文本框的值 <input name='formnickname' class="textarea" placeholder=" ...
- Python从菜鸟到高手(6):获取用户输入、函数与注释
1. 获取用户输入 要编写一个有实际价值的程序,就需要与用户交互.当然,与用户交互有很多方法,例如,GUI(图形用户接口)就是一种非常好的与用户交互的方式,不过我们先不讨论GUI的交互方式,本节会 ...
随机推荐
- ipod不识别命令
root@mx6q:~# history 0 sync 1 reboot 2 cd /usr/app/ 3 ls 4 cd extend-sysfs/ 5 ls 6 cd etc/ 7 ls 8 cd ...
- 20145314郑凯杰《网络对抗技术》实验1 逆向及Bof基础实践
20145314郑凯杰<网络对抗技术>实验1 逆向及Bof基础实践 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数 ...
- 初识PHP(一)基础语法
一直准备学习PHP,结果前一段时间总是有事情,耽误了一阵子.现在赶快迎头赶上! 这个系列只是谈谈我对于PHP的一些看法,不是教程性质的.另外我是小白,只是写写随笔,大神求轻拍.本人学习过c .java ...
- 框架-spring入门总结
框架-spring入门总结 参考: http://www.cnblogs.com/heavenyes/p/3908546.html http://www.cnblogs.com/heavenyes/p ...
- CCleaner如何禁用开机自动启动
https://forum.piriform.com/topic/42073-ccleaner-starts-on-startup/ 在options-->setting里面选择开机不启动 在O ...
- fiddler几种功能强大的用法(二)
参考网址:http://blog.rekfan.com/articles/228.html http://www.cnblogs.com/tugenhua0707/p/4637771.html htt ...
- c#pdf查看器
Free Spire.PDF for .NET is a Community Edition of the Spire.PDF for .NET, which is a totally free PD ...
- 11_MySQL_分页查询
# 分页查询/* 应用场景:要显示的数据,一页显示不全,需要分页提交sql请求 语法: select 查询列表 from 表 [join type]表2 on 连接条件 where 筛选条件 grou ...
- Gym 101246H ``North-East''(LIS)
http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的 ...
- shell 判断文件是否存在,没有则创建
没有该文件则创建,有则 ls -l 输出文件信息. #!/bin/bash echo "enter the name:" read filename if test -e $fil ...