shell使用read命令来接受输入

1. 使用read命令来接受输入

shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入获得

使用read来把输入值分配给一个或多个shell变量,read从标准输入中读取值,给每个单词分配一个变 量,所有剩余单词都被分配给最后一个变量,如果变量名没有指定,默认标准输入的值赋值给系统内置 变量REPLY

格式

read [选项][变量名......]

常用选项

选项 说明
-p prompt 设置提示信息
-s 静默输入,一般用于密码
-n N 指定输入的字符长度N
-d '字符' 输入结束符
-t timeout 设置输入等待的时间,单位默认是秒

示例

[root@centos8 ~]#read
song
[root@centos8 ~]#echo $REPLY
song
[root@centos8 ~]#read NAME TITLE
song sre
[root@centos8 ~]#echo $NAME
song
[root@centos8 ~]#echo $TITLE
sre
[root@centos8 ~]#read -p "Please input your name:" NAME
Please input your name:song
[root@centos8 ~]#echo $NAME
song
[root@centos8 ~]#

示例

[root@centos8 ~]#read x y z <<< "I Love SRE"
[root@centos8 ~]#echo $x
I
[root@centos8 ~]#echo $y
Love
[root@centos8 ~]#echo $z
SRE
[root@centos8 ~]#

示例

# Pipelines:A pipeline is a sequence of one or more commands separated by one of
the control operators | or |&

[root@centos8 ~]#echo sre |read job
[root@centos8 ~]#echo $job

[root@centos8 ~]#echo sre | { read job; echo $job; }
sre
[root@centos8 ~]#

示例:面试题:read 和输入重定向

[root@centos8 ~]#cat test.txt
1 2
[root@centos8 ~]#read i j < test.txt ; echo i=$i j=$j
i=1 j=2



[root@centos8 ~]#echo 1 2 | read x y ; echo x=$x y=$y
x= y=
[root@centos8 ~]#echo 1 2 | ( read x y ; echo x=$x y=$y )
x=1 y=2
[root@centos8 ~]#echo 1 2 | { read x y ; echo x=$x y=$y; }
x=1 y=2
[root@centos8 ~]#

示例:判断用户输入的是否为 YES

[root@centos8 ~/script]#cat read.sh
#!/bin/bash
#
read -p "Are you rich? yes or no: " ANSWER
[[ $ANSWER =~ ^([Yy]|[Yy][Ee][Ss])$ ]] && echo "You are rich" || echo "Good Good Study Day Day up"
[root@centos8 ~/script]#bash read.sh
Are you rich? yes or no: y
You are rich
[root@centos8 ~/script]#bash read.sh
Are you rich? yes or no: Y
You are rich
[root@centos8 ~/script]#bash read.sh
Are you rich? yes or no: yes
You are rich
[root@centos8 ~/script]#bash read.sh
Are you rich? yes or no: fsdfsdf
Good Good Study Day Day up
[root@centos8 ~/script]#

示例:判断用户输入的是否为 YES

[root@centos8 ~/script]#cat yesorno.sh
#!/bin/bash
#
read -p "Please input yes or no: " input

answer=$(echo $input| tr 'A-Z' 'a-z')

[ $answer = 'yes' -o $answer = 'y' ] && echo YES

[ $answer = 'no' -o $answer = 'n' ] && echo NO

[root@centos8 ~/script]#cat yesorno2.sh
#!/bin/bash
#
read -p "Please input yes or no: " input

[[ $input =~ ^([Yy]|[Yy][Ee][Ss])$ ]] && echo YES

[[ $input =~ ^([Nn]|[Nn][Oo])$ ]] && echo NO
[root@centos8 ~/script]#

示例:鸡兔同笼算法,今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?

[root@centos8 ~/script]#cat chook_rabbit.sh
#!/bin/bash
#
read -p "Please Enter Head Number: " HEAD
read -p "Please Enter Foot Number: " FOOT
RABBIT=$[FOOT/2-HEAD]
CHOOK=$[HEAD-RABBIT]
echo "rabbit:" $RABBIT
echo "chook:" $CHOOK
[root@centos8 ~/script]#

示例

read -t 5 -p "please input two number:" var1 var2
echo  "The nums you input is:" $var1 $var2
[ -z $var1 ] && echo "var is not exist"
[ -z $var2 ] && echo "var is not exist"

# -t timeout 设置输入等待的时间,单位默认是秒
echo -n "please input two number:"
read -t 5 var1 var2

示例

cat menu.sh
menu(){
cat << EOF
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    please input then num that:
EOF         # 顶格写
}
menu    
read num
echo "you have selected $num"
[ $num -eq 1 ] && {
    echo "starting install lamp"
    /bin/sh /server/scripts/lamp.sh
    exit 1
}
[ $num -eq 2 ] && {
    echo "starting install lnmp"
    /bin/sh /server/scripts/lnmp.sh
    exit 1
}
[ $num -eq 3 ] && {
    echo "this scripts is about ro logout"
    exit 1
}
[ ! $num - eq 1 -o ! $num -eq 2 -o ! $num -eq 3 ] &&{
    echo "bye"
    exit 1
}
posted @ 2021-04-09 20:39  临江仙我亦是行人  阅读(266)  评论(0编辑  收藏  举报