read是一个重要的bash命令,它用于从键盘或标准输入中读取文本。可以使用read以交互的形式读取来自用户的输入,不过read能做的远不止这些。很多编程语言的输入库都是从键盘读取输入,且只有回车键按下的时候,才标志着输入完毕,很多情形不是这样的,比如输入结束与否是基于字符数或某个特定字符来决定的。

read命令提供了一种不需要按回车键就能搞定任务的方法。

1、借助read的各种选项来实现不同的效果。

下面的语句从输入中读取n个字符并存入变量variable_name;

read -n number_of_chars variable_name

eg:

$ read -n 2 var

$ echo $var

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

read -s var

3)显示提示信息:

read -p "Enter input:" var

4)在特定时限内读取输入:

read -t timeout var

如:$read -t 2 var  #在2秒内将键入的字符串读入变量var

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

read -d delim_char var

eg:$ read -d ":" var

$hello:#var被设置为hello

1.14不使用回车键来读取n个字符的更多相关文章

  1. [LeetCode] Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  2. [Swift]LeetCode157.用Read4来读取N个字符 $ Read N Characters Given Read4

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  3. [Swift]LeetCode158. 用Read4来读取N个字符II $ Read N Characters Given Read4 II

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  4. [LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  5. GOM通区插件-支持GOM绝对路径-读取配置项-分割字符等功能。不定期更新

    A-A+ 2019年07月19日 Gom引擎 阅读 45 views 次   [@Main] #IF #SAY [<读配置项/@读配置项>] [<写配置项/@写配置项>] [& ...

  6. 常见错误0xCCCCCCCCC 读取字符串的字符时出错及其引申。

    问题描述在一个函数调用中,使用二级指针作为函数参数,传递一个字符串指针数组,但是在访问的时候,却出现了运行时错误,具体表现为"0xCCCCCCCC 读取字符串的字符时出错". 第一 ...

  7. 14.swoole学习笔记--异步读取文件

    <?php //异步读取文件 swoole_async_readfile(__DIR__."/1.txt",function($filename,$content){ ech ...

  8. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  9. Unicode文件读取 出现隐藏字符 (大坑)

    C#读取文件..分析时发现应该15位的.. str.Lenght 却 16位.. 字符串复制出来一位位的数..就是15位.. 纳闷中突然想起来会不会是隐藏字符.. 输出 str[0].ToBytes( ...

随机推荐

  1. Upgrading Elasticsearch

    Upgrading Elasticsearch | Elasticsearch Reference [5.6] | Elastic https://www.elastic.co/guide/en/el ...

  2. (C)位字段(bit-field)

    位字段(bit-field) 在存储空间很宝贵的情况下,有可能需要将多个对象保存在一个机器字中,一种常用的方法是:使用类似于编译器符号表的单个二进制位标志集合,外部强加的数据格式(如设备接口等寄存器) ...

  3. P3968 [TJOI2014]电源插排

    P3968 [TJOI2014]电源插排 线段树维护最长空区间及左端点位置,这个和$nlongn$的动态最大子序和差不多,就不多解释了 $n$较大哈希优化空间 My complete code: #i ...

  4. Linux下新建oracle用户

    su - oraclesqlplus / as sysdba the procedure of dropping user are as follow: select sid,serial# from ...

  5. zabbix api支持的数据类型

    bool flag integer float string timestamp array object query countOutput editable excludeSearch filte ...

  6. Go丨语言package github.com/Go-SQL-Driver/MySQL: exec: "git": executable file not found in %PATH%解决方法

    Go语言在添加第三方MySQL驱动的时候报错: go: missing Git command. See https://golang.org/s/gogetcmd package github.co ...

  7. codeforces 569C C. Primes or Palindromes?(素数筛+dp)

    题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...

  8. linux 进程学习笔记-named pipe (FIFO)命名管道

    与“无名管道”不同的是,FIFO拥有一个名称来标志它,所谓的名称实际上就是一个路径,比如“/tmp/my_fifo”,其对应到磁盘上的一个管道文件,如果我们用file命令来查看其文件类型的话,会得到如 ...

  9. bjwc Day2 玄学

    早晨起来很开心,因为昨天跟妹子聊天聊到很晚 然后看到了题,感觉:这tm才是冬令营呀! T1构造,并没有找到性质,暴力都懒得打 T2数位dp,状态比较麻烦,看来跟dmy想到一起了,然后搞一下搞完 T3放 ...

  10. @Autowired的作用,自动装配,省去写get/set方法

    这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get. <bean id="userDao" clas ...