shell read 交互

4 篇文章 0 订阅
read 交互 
[root@cdh1 shell-test]# vim read.sh
[root@cdh1 shell-test]# vim read.sh
[root@cdh1 shell-test]# ll
total 52
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root  201 Jul 14 19:04 2
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  163 Jul 14 19:16 echoTest1.sh
-rwxr-xr-x 1 root root  183 Jul 14 19:24 echoTest2.sh
-rwxr-xr-x 1 root root  149 Jul 14 03:15 echoTest.sh
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
-rwxr-xr-x 1 root root  201 Jul 14 19:04 para.sh
-rw-r--r-- 1 root root  539 Jul 14 19:47 read.sh
-rwxr-xr-x 1 root root  118 Jul 14 02:52 sum.sh
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# chmod a+x read.sh
[root@cdh1 shell-test]# ll
total 52
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root  201 Jul 14 19:04 2
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  163 Jul 14 19:16 echoTest1.sh
-rwxr-xr-x 1 root root  183 Jul 14 19:24 echoTest2.sh
-rwxr-xr-x 1 root root  149 Jul 14 03:15 echoTest.sh
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
-rwxr-xr-x 1 root root  201 Jul 14 19:04 para.sh
-rwxr-xr-x 1 root root  539 Jul 14 19:47 read.sh
-rwxr-xr-x 1 root root  118 Jul 14 02:52 sum.sh
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# sh read.sh
Please
read.sh: line 3: read: `name:': not a valid identifier
Name is
Please enter your age: n
Age is
Please select your gender[M/F]: 
n
Sex is
[root@cdh1 shell-test]# cat read.sh
#!/bin/bash
# Author: shenchao (E-mail: shenchao@lampbrother.net)
read -t 30 -p Please input your name:  name
#提示“请输入姓名”并等待30秒,把用户的输入保存入变量name中
echo Name is $name 
read -s -t 30 -p "Please enter your age: " age
#年龄是隐私,所以我们用“-s”选项隐藏输入
echo -e \n
echo Age is $age 
read -n 1 -t 30 -p "Please select your gender[M/F]: " gender
#使用“-n 1”选项只接收一个输入字符就会执行(都不用输入回车)
echo -e \n
echo Sex is $gender
[root@cdh1 shell-test]# cp read.sh read1.sh 
[root@cdh1 shell-test]# ll
total 56
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root  201 Jul 14 19:04 2
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  163 Jul 14 19:16 echoTest1.sh
-rwxr-xr-x 1 root root  183 Jul 14 19:24 echoTest2.sh
-rwxr-xr-x 1 root root  149 Jul 14 03:15 echoTest.sh
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
-rwxr-xr-x 1 root root  201 Jul 14 19:04 para.sh
-rwxr-xr-x 1 root root  539 Jul 14 19:50 read1.sh
-rwxr-xr-x 1 root root  539 Jul 14 19:47 read.sh
-rwxr-xr-x 1 root root  118 Jul 14 02:52 sum.sh
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# vim read1.sh
[root@cdh1 shell-test]# sh read1.sh 
Please input your name:
Name is
Please enter your age: n
Age is
Please select your gender[M/F]: 
n
Sex is
[root@cdh1 shell-test]# cat read1.sh 
#!/bin/bash
# -p 选项 默认以空格 匹配提示语,如果 提示内容较长 有空格 需要加引号.
read -t 30 -p "Please input your name:"  name
#提示“请输入姓名”并等待30秒,把用户的输入保存入变量name中
echo Name is $name 
read -s -t 30 -p "Please enter your age: " age
#年龄是隐私,所以我们用“-s”选项隐藏输入
echo -e \n
echo Age is $age 
read -n 1 -t 30 -p "Please select your gender[M/F]: " gender
#使用“-n 1”选项只接收一个输入字符就会执行(都不用输入回车)
echo -e \n
echo Sex is $gender
[root@cdh1 shell-test]# vim read1.sh
[root@cdh1 shell-test]# sh read1.sh 
Please input your name:
Name is
Please enter your age: n
Age is
Please select your gender[M/F]: 


Sex is
[root@cdh1 shell-test]# cat read1.sh 
#!/bin/bash
# -p 选项 默认以空格 匹配提示语,如果 提示内容较长 有空格 需要加引号.
read -t 30 -p "Please input your name:"  name
#提示“请输入姓名”并等待30秒,把用户的输入保存入变量name中
echo Name is $name 
read -s -t 30 -p "Please enter your age: " age
#年龄是隐私,所以我们用“-s”选项隐藏输入
echo -e \n
echo Age is $age 
read -n 1 -t 30 -p "Please select your gender[M/F]: " gender
#使用“-n 1”选项只接收一个输入字符就会执行(都不用输入回车)
echo -e "\n"
echo Sex is $gender
[root@cdh1 shell-test]# sh read1.sh 
Please input your name:lyc
Name is lyc
Please enter your age: n
Age is 181818
Please select your gender[M/F]: m

Sex is m
[root@cdh1 shell-test]# 
[root@cdh1 shell-test]# sh read1.sh 
Please input your name:lyc
Name is lyc
Please enter your age: n
Age is 18
Please select your gender[M/F]: M

Sex is M
[root@cdh1 shell-test]# cat read1.sh 
#!/bin/bash
# -p 选项 默认以空格 匹配提示语,如果 提示内容较长 有空格 需要加引号.
read -t 30 -p "Please input your name:"  name
#提示“请输入姓名”并等待30秒,把用户的输入保存入变量name中
echo Name is $name 
read -s -t 30 -p "Please enter your age: " age
#年龄是隐私,所以我们用“-s”选项隐藏输入
echo -e \n
echo Age is $age 
read -n 1 -t 30 -p "Please select your gender[M/F]: " gender
#使用“-n 1”选项只接收一个输入字符就会执行(都不用输入回车)
echo -e "\n"
echo Sex is $gender
[root@cdh1 shell-test]# vim read1.sh
[root@cdh1 shell-test]# sh read1.sh 
Please input your name:lyc
Name is lyc
Please enter your age: 

Age is 20
Please select your gender[M/F]: M

Sex is M
[root@cdh1 shell-test]# cat read1.sh 
#!/bin/bash
# -p 选项 默认以空格 匹配提示语,如果 提示内容较长 有空格 需要加引号.
read -t 30 -p "Please input your name:"  name
#提示“请输入姓名”并等待30秒,把用户的输入保存入变量name中
echo Name is $name 
read -s -t 30 -p "Please enter your age: " age
#年龄是隐私,所以我们用“-s”选项隐藏输入
echo -e "\n"
echo Age is $age 
read -n 1 -t 30 -p "Please select your gender[M/F]: " gender
#使用“-n 1”选项只接收一个输入字符就会执行(都不用输入回车)
echo -e "\n"
echo Sex is $gender
[root@cdh1 shell-test]# 





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

5icode.top

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值