Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)(二)

2014-11-24 02:38:33 · 作者: · 浏览: 1
作用。\\表示一个\号,\表示续行,\代表本身。
格式:read VAR
格式:read -p VAR

[root@node56 ~]# read VAR
hello world
[root@node56 ~]# echo $VAR
hello world
[root@node56 ~]# read -p "Input your number: " VAR
Input your number: 123
[root@node56 ~]# echo $VAR
123
[root@node56 ~]# read VAR
yes\tno
[root@node56 ~]# echo $VAR
yestno
[root@node56 ~]#


读取一行文本,但是取消反斜杠的转义作用。
格式:read -r VAR
格式:read -p -r VAR
man bash: read 写道
-r Backslash does not act as an escape character. The backslash is considered to be part of the
line. In particular, a backslash-newline pair may not be used as a line continuation.

[root@node56 ~]# read -r VAR
yes\tno
[root@node56 ~]# echo $VAR
yes\tno
[root@node56 ~]#

关于read -p
此参数用于在读取变量之前显示提示信息。如果输入重定向了,就不会显示,比如重定向从文件中读取。
也就是说,read -p PROMPT VAR 不等同于 echo PROMPT; read VAR 的组合。
man bash: read 写道
-p prompt
Display prompt on standard error, without a trailing newline, before attempting to read any
input. The prompt is displayed only if input is coming from a terminal.

读取密码(输入的字符不回显)
格式:read -s PASSWORD
格式:read -p -s PASSWORD
man bash: read 写道
-s Silent mode. If input is coming from a terminal, characters are not echoed.

[root@node56 ~]# echo $PASSWORD

[root@node56 ~]# read -s PASSWORD
[root@node56 ~]# echo $PASSWORD
12345
[root@node56 ~]# read -p "Input password: " -s PASSWORD
Input password:
[root@node56 ~]# echo $PASSWORD
54321

读取指定数量字符
格式:read -n VAR
格式:read -p -n VAR
man bash: read 写道
-n nchars
read returns after reading nchars characters rather than waiting for a complete line of input.

[root@node56 ~]# read -p "Input two chars: " -n 2 VAR
Input two chars: 12 [root@node56 ~]# echo $VAR
12
[root@node56 ~]#

在指定时间内读取
格式:read -t VAR
格式:read -p -t VAR

man bash: read 写道
-t timeout
Cause read to time out and return failure if a complete line of input is not read within timeout
seconds. This option has no effect if read is not reading input from the terminal or a pipe.

[root@node56 ~]# read -p "Input some words in 5 seconds: " -t 5 VAR
Input some words in 5 seconds: [root@node56 ~]#

从文件中读取
格式:read VAR 对于read命令,可以指定-r参数,避免\转义。
格式:read -r VAR 错误:cat file.txt | read VAR (此处read命令将会在子进程中执行,子进程无法更改父进程的变量)

[root@web ~]# cat input.txt
Some text here
with backslash \ here
dollar $HOME meet

[root@web ~]# cat input.txt | read VAR
[root@web ~]# echo $VAR

[root@web ~]# read VAR [root@web ~]# echo $VAR
Some text here
[root@web ~]# read VAR1 VAR2 VAR3 VAR4 [root@web ~]# echo "VAR1=$VAR1 VAR2=$VAR2 VAR3=$VAR3 VAR4=$VAR4"
VAR1=Some VAR2=text VAR3=here VAR4=
[root@web ~]# read VAR1 VAR2 [root@web ~]# echo "VAR1=$VAR1 VAR2=$VAR2"
VAR1=Some VAR2=text here
[root@web ~]#
上面的直接对read命令输入重定向,只能读取输入文件中的一行。

如果需要读取整个文件,最好将其写在一个代码块中。
Advanced Bash-Scripting Guide: Chapter 3. Special Characters {} 写道
Block of code [curly brackets]. Also referred to as an inline group, this construct, in effect, creates an anonymous function (a function without a name). However, unlike in a "standard" function, the variables inside a code block remain visible to the remainder of the script.

The code block enclosed in braces may have I/O redirected to and from it.

Unlike a command group within (parentheses), as above, a code block enclosed by {braces} will not normally launch a subshell.


{ read LINE1; read LINE2; read LINE3; } 注意每个read命令之后都要以分号结束。

[root@web ~]# { read LINE1; read LINE2; read LINE3; } [root@web ~]# echo $LINE1
Some text here
[root@web ~]# echo $LINE2
with backslash here
[root@web ~]# echo $LINE3
dollar $HOME meet
[root@web ~]#
上面LINE2中\ 没有读取出来,因为在没有加上-r参数时,read会转义。

现在加上-r参数来测试一下。
{ read -r LINE1; read -r LINE2;