码迷,mamicode.com
首页 > 系统相关 > 详细

shell read 交互

时间:2016-07-15 21:20:45      阅读:313      评论:0      收藏: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]# 





shell read 交互

标签:

原文地址:http://blog.csdn.net/hzdxw/article/details/51915835

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!