read 用户输入值

最后更新于:2022-04-02 03:53:31

[TOC] ## read 输入单个 ``` #!/bin/bash echo -n "输入一些文本 > " read text echo "你的输入:$text" ``` 输入多个值,以空格隔开 ``` #!/bin/bash echo Please, enter your firstname and lastname read FN LN echo "Hi! $LN, $FN !" ``` read命令之后没有定义变量名,则全部都是 ``` #!/bin/bash echo -n "Enter one or more values > " read echo "REPLY = '$REPLY'" ``` ``` 输出 REPLY = 'helo as das d asd as d ad' 读取文件内容 ``` ### -t 参数,设置超市时间 ``` #!/bin/bash echo -n "输入一些文本 > " if read -t 3 response; then echo "用户已经输入了" else echo "用户没有输入" fi //or,设置在变量中设置过期时间 $ TMOUT=3 $ read response ``` ### -p 参数 ,显示信息提示 ``` read -p "Enter one or more values > " echo "REPLY = '$REPLY'" ``` ### -a参数把用户的输入赋值给一个数组 ``` $ read -a people alice duchess dodo $ echo ${people[2]} dodo ``` ### -e 参数 在按tab 时候,可以进行自动补齐 ``` #!/bin/bash echo Please input the path to the file: read -e fileName echo $fileName ``` ### -s:不显示在屏幕,用于输入密码或保密信
';