zoukankan      html  css  js  c++  java
  • Linux shell read 解析

    read是一个重要的bash命令,它用于从键盘或标准输入中读取文本,我们可以用read以交互的方式读取来自用户的输入,不过read能做的可远不止这些,当从键盘读取用户输入的时候,只有按下回车键才标志输入结束,但是很多时候是没办法按回车键的,输入结束与否是由某个特定字符决定的,例如:在一个游戏当中,当按下+键的时候,小球会向上移动。那么若是每次按+键再按回车键,未免太低效了。read命令提供了一种不需要按回车键就能搞定这个任务的方法。

    1. 从下面的语句输入中读取n个字符并存入变量var中

    1 [wandl@TestMachine2 shellScript]$ read -n 3 var
    2 123[wandl@TestMachine2 shellScript]$ echo $var
    3 123
    4 #输入第三个字符后直接结束输入

    2.用无回显的方式读取密码

    1 [wandl@TestMachine2 shellScript]$ read -s var
    2 [wandl@TestMachine2 shellScript]$ echo $var
    3 thisispassword
    #在输入字符时,不显示

    3.显示提示信息

    1 [wandl@TestMachine2 shellScript]$ read -p "Pls enter your name:" var
    2 Pls enter your name:creazy
    3 [wandl@TestMachine2 shellScript]$ echo $var
    4 creazy

    4.在特定时限内读取输入

    1 [wandl@TestMachine2 shellScript]$ read -t 3 var
    2 qw
    3 [wandl@TestMachine2 shellScript]$ echo $var
    4 qw
    5 #在3秒钟内输入并且按回车键

    5.用特定的定界符作为输入行的结束

    1 [wandl@TestMachine2 shellScript]$ read -d "q" var
    2 weq[wandl@TestMachine2 shellScript]$ echo $var
    3 we
  • 相关阅读:
    Cmder安装和设置
    php7.x版本的新特性
    【Luogu】P4916 [MtOI2018]魔力环 题解
    Codeforces 1530E Minimax 题解
    昭阳E42-80 屏幕不亮,风扇狂转
    iPad宽高像素值
    关于UIView的autoresizingMask属性的研究
    判断单链表中是否存在环及查找环的入口点
    网络编程
    事件响应者链的工作原理
  • 原文地址:https://www.cnblogs.com/nurruden/p/6945148.html
Copyright © 2011-2022 走看看