linux shell获取用户输入
来源:互联网 发布:java单例模式的优点 编辑:程序博客网 时间:2024/05/18 04:57
一、获取用户输入
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
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
阅读全文
0 0
- linux shell获取用户输入
- shell 获取用户输入
- linux shell读取用户输入
- linux shell 处理用户输入
- Linux Shell 读取用户输入
- Shell脚本基础3-获取用户输入
- linux shell获取用户输入指定范围的单个字符的两种方法
- Linux Shell基础知识8 用户输入
- Shell读取用户输入
- shell 读取用户输入
- Shell读取用户输入
- 【shell】处理用户输入
- Linux学习-shell脚本编程基础之处理用户输入
- linux shell脚本读取用户输入的参数
- 【Linux】shell---判断用户输入的是否为文件
- linux shell脚本读取用户输入的参数
- Linux命令行与shell编程笔记-13处理用户输入
- linux shell 学习之第13章 处理用户输入
- mysql 中find_in_set()和in()用法比较
- Yii2 环境配置生产环境和测试环境
- 数据结构复习——链栈的一些操作以及表达式中的括号是否配对问题
- yii2 数据库查询
- yii2 笔记(1)
- linux shell获取用户输入
- php脚本#!/usr/bin/env php写法的好处
- shell for循环
- MFC之旅——SelectClipPath()函数的特性
- JSF 退出登录时删除session
- php 自定义函数大全
- linux 自启动tomcat服务
- 特殊字符大全
- css:div垂直居中