频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
2011-09-14 14:12:49           
收藏   我要投稿

In Java
Scanner类:Scanner.hasNext() & Scanner.next() & Scanner.hasNextLine() & Scanner.nextLine()
JavaDoc class Scanner 写道
A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

A scanning operation may block waiting for input.

The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.
 
下面是关于 Scanner.hasNext() 和 Scanner.next() 的例子,用于读取以空白分隔的标识符。
Java代码 
Scanner scanner = new Scanner(System.in); 
while (scanner.hasNext()) { 
   String msg = scanner.next(); 
   System.out.println(msg); 

 
下面是关于 Scanner.hasNextLine() 和 Scanner.nextLine() 的例子,用于读取输入数据行。
Java代码 
Scanner scanner = new Scanner(System.in); 
while (scanner.hasNextLine()) { 
   String msg = scanner.nextLine(); 
   System.out.println(msg); 

 
更详细的说明请参考:http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html
 
Console类:System.console() & Console.readLine() & Console.readPassword()
Console类是JDK6加入的,它是对控制台的封装。使用 System.console() 可以获得与当前Java虚拟机关联的控制台,如果有的话。Console.readLine可以读取一行,Console.readPassword可以读取密码。
 
JavaDoc class Console 写道
public final class Console
extends Object
implements Flushable

Methods to access the character-based console device, if any, associated with the current Java virtual machine.

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

 String     readLine()
          Reads a single line of text from the console.

 char[]     readPassword()
          Reads a password or passphrase from the console with echoing disabled
 
下面是一个关于Console的示例代码:
 
Java代码 
Console console = System.console(); // 获得Console实例对象      
if (console != null) {              // 判断是否有控制台的使用权      
    String user = new String(console.readLine("Enter username:"));      // 读取整行字符      
    String pwd = new String(console.readPassword("Enter passowrd:"));   // 读取密码,输入时不显示      
    console.printf("Username is: " + user + "\n");      // 显示用户名      
    console.printf("Password is: " + pwd + "\n");   // 显示密码      
} else {      
    System.out.println("Console is unavailable.");  // 提示无控制台使用权限      

 
更详细的说明请参考:·http://download.oracle.com/javase/6/docs/api/java/io/Console.html
 
In Bash
从标准输入读取
使用read命令读取字符串到变量中。但是,如果有反斜杠,将起到转义的作用。\\表示一个\号,\<newline>表示续行,\<char>代表<char>本身。
格式:read VAR
格式:read -p <prompt> 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 <prompt> -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 <prompt> -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 <nchars> VAR
格式:read -p <prompt> -n <nchars> 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 <seconds> VAR
格式:read -p <prompt> -t <seconds> 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 <file.txt
对于read命令,可以指定-r参数,避免\转义。
格式:read -r VAR <file.txt
错误: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 <input.txt
[root@web ~]# echo $VAR
Some text here
[root@web ~]# read VAR1 VAR2 VAR3 VAR4<input.txt
[root@web ~]# echo "VAR1=$VAR1 VAR2=$VAR2 VAR3=$VAR3 VAR4=$VAR4"
VAR1=Some VAR2=text VAR3=here VAR4=
[root@web ~]# read VAR1 VAR2<input.txt
[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; } <input.txt
注意每个read命令之后都要以分号结束。
 
[root@web ~]# { read LINE1; read LINE2; read LINE3; } <input.txt
[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; read -r LINE3; } <input.txt
 
[root@web ~]# { read -r LINE1; read -r LINE2; read -r LINE3; } <input.txt
[root@web ~]# echo $LINE1
Some text here
[root@web ~]# echo $LINE2
with backslash \ here
[root@web ~]# echo $LINE3
dollar $HOME meet
[root@web ~]#
 
从Here Document读取
错误:read VAR <<EOF
some string
EOF
正确:{ read VAR; } <<EOF
some string
EOF
问题:为什么写在代码块中才能读取呢?
 
[root@web ~]# read VAR <<EOF
> some string
> EOF
[root@web ~]# echo "$VAR"

[root@web ~]# { read VAR; } <<EOF
> hello
> EOF
[root@web ~]# echo "$VAR"        
hello
[root@web ~]#
 
从Here String读取
read VAR <<< "some string"
read VAR <<< "$STR"
 
[root@web ~]# read VAR <<< "some string"
[root@web ~]# echo $VAR
some string
[root@web ~]#
 
 
补充:关于 cat input.txt | read VAR 的问题
先说一下我对管道线的理解:管道线两侧的命令,前一个命令的输出(标准输出),将作为后一个命令的输入(标准输入)。两侧的命令都是在子进程中执行的,都有各自独立的地址空间,子进程会继承(复制)父进程所有的环境变量。子Shell(subshell)是一种特殊的子进程,它会继承(复制)父Shell的所有变量;对于非子Shell进程(外部命令),只有环境变量会被继承(复制)。但是,子进程一旦启动,它所有的变量只在它的进程空间中有效,它对变量的修改都是对自己范围之内的变量的修改,对父进程相同名称的变量是没有影响的。而子进程启动之后,它的父进程对父进程变量的修改,对子进程相同名称的变量也是没有影响的。
Advanced Bash-Scripting Guide: Chapter 3. Special Characters | 写道
|

pipe. Passes the output (stdout) of a previous command to the input (stdin) of the next one, or to the shell. This is a method of chaining commands together.

A pipe, as a classic method of interprocess communication, sends the stdout of one process to the stdin of another. In a typical case, a command, such as cat or echo, pipes a stream of data to a filter, a command that transforms its input for processing.

The output of a command or commands may be piped to a script.

A pipe runs as a child process, and therefore cannot alter script variables.

variable="initial_value"
echo "new_value" | read variable
echo "variable = $variable" # variable = initial_value
 
[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
 
上面的cat input.txt的输出将作为read VAR的输入。
[root@web ~]# cat input.txt | { read VAR; echo $VAR; }
Some text here
可以看到管道线后面的子Shell中确实读到了值。
[root@web ~]# echo $VAR
 
但父Shell中的相同变量不会改变。
[root@web ~]#

作者“Bash @ Linux
 

点击复制链接 与好友分享!回本站首页
相关TAG标签 字符串
上一篇:Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
下一篇:Bash字符串处理(与Java对照) - 4.字符串输出
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站