您的位置:威尼斯官方网站 > 威尼斯官方网站 > 1、从正式输入读入

1、从正式输入读入

发布时间:2019-11-06 21:41编辑:威尼斯官方网站浏览(78)

    主干用法

        read命令主要用来从正式输入读取内容或从文件中读取内容,并把音信保存到变量中。其常用用法如下所示:

    read [选项] [文件]
    
    选项 解释
    -a array 将内容读取到数值中,变量默认为数组且以空格做为分割符
    -d delimiter 遇到指定的字符即停止读取
    -n nchars 指定最多可以读入的字符数,即定义输入文本的长度
    -r 屏蔽转义符
    -p prompt 显示提示信息
    -s 静默模式,在输入字符时不在终端中显示,常用于密码输入等
    -t timeout 指定超时时间
    -u FD 从文件描述符中读入,该FD可以由exec开启

    用法示例

    1、从规范输入读入

    [root@localhost test]# cat read.sh
    #!/bin/bash
    echo -n "Please input your name:"
    read name
    echo "Hello $name"
    [root@localhost test]# bash read.sh
    Please input your name:Jack
    Hello Jack
    

    2、钦定显示音讯从专门的学业输入读入

    [root@localhost test]# cat read.sh
    #!/bin/bash
    # echo -n "Please input your name:"
    read -p "Please input your name:" name
    # read name
    echo "Hello $name"
    [root@localhost test]# bash read.sh
    Please input your name:Jack
    Hello Jack
    

    在上述示例中,read是二遍能够选用三个参数的,如下所示:

    read -p "Please input your name:" firstName secondName lastName
    

    但必要留意的事项如下:

    • 假使输入的多寡少于变量个数,则多余的变量不会收获到数码,即变量值为空
    • 假使输入的数码多于变量个数,则抢先的数额将全部赋给最终二个变量
    • 倘诺在read命令后边未有概念任何变量,脚本在推行时,如若顾客输入数据,这个时候数量则保留随处境变量$REPLY

    3、钦定超时时间

    [root@localhost test]# cat read.sh
    #!/bin/bash
    if read -t 3 -p "Please input your name:" firstName secondName lastName
    then
      echo "variable is $firstName - $secondName - $lastName"
    else
       echo -e  "ntimeoutn"
    fi
    [root@localhost test]# bash read.sh
    Please input your name:
    timeout
    

    4、从钦点文件中读取内容

    [root@localhost test]# cat -n test.txt
         1  this is test text.
         2  this is second line.
         3  Hello world
         4  C# Main
         5  Python
    # 使用-u选项
    [root@localhost test]# cat readtest.sh
    #!/bin/bash
    exec 5< ~/test/test.txt
    count=0
    while read -u 5 var
    do
     let count=$count+1
     echo "Line $count is $var"
    done
    echo "Total line count is $count"
    exec 5<&-
    [root@localhost test]# bash readtest.sh
    Line 1 is this is test text.
    Line 2 is this is second line.
    Line 3 is Hello world
    Line 4 is C# Main
    Line 5 is Python
    Total line count is 5
    # 使用管道
    [root@localhost test]# cat readtest.sh
    #!/bin/bash
    count=1
    cat ~/test/test.txt |  while read line
    do
     echo "Current line $count - $line "
     let count=$count+1
    done
    echo "Total line count is $count"
    [root@localhost test]# bash readtest.sh
    Current line 1 - this is test text.
    Current line 2 - this is second line.
    Current line 3 - Hello world
    Current line 4 - C# Main
    Current line 5 - Python
    Total line count is 1
    # 使用重定向
    [root@localhost test]# cat readtest.sh
    #!/bin/bash
    count=0
    while read line
    do
     let count=$count+1
     echo "Current line $count - $line "
    done < ~/test/test.txt
    echo "Total line count is $count"
    [root@localhost test]# bash readtest.sh
    Current line 1 - this is test text.
    Current line 2 - this is second line.
    Current line 3 - Hello world
    Current line 4 - C# Main
    Current line 5 - Python
    Total line count is 5
    

    正文同步在Wechat订阅号上发布,如各位小友大家喜欢本人的篇章,也得以关切自身的Wechat订阅号:woaitest,或扫描下边包车型地铁二维码增添关怀:
    图片 1

    本文由威尼斯官方网站发布于威尼斯官方网站,转载请注明出处:1、从正式输入读入

    关键词:

上一篇:没有了

下一篇:没有了