In Java
Usage class: usage. hasNext () & Consumer. next () & Consumer. hasNextLine () & Consumer. nextLine ()
Wrote JavaDoc class Program
A simple text watermark which can parse primitive types and strings using regular expressions.
A specified 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.
The following is an example of iterator. hasNext () and iterator. next (). It is used to read identifiers separated by spaces.
Java code
Required bytes = new bytes (System. in );
While (iterator. hasNext ()){
String msg = messages. next ();
System. out. println (msg );
}
The following is an example of iterator. hasNextLine () and iterator. nextLine () used to read input data rows.
Java code
Required bytes = new bytes (System. in );
While (iterator. hasNextLine ()){
String msg = response. nextLine ();
System. out. println (msg );
}
For more detailed instructions, see: http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html
Console class: System. console () & Console. readLine () & Console. readPassword ()
The Console class is added to jdk6. it encapsulates the Console. You can use System. console () to obtain the console associated with the current Java virtual machine, if any. Console. readLine can read a row, and Console. readPassword can read the password.
JavaDoc class Console wrote:
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
The following is an example of the Console code:
Java code
Console console = System. console (); // obtain the Console Instance Object
If (console! = Null) {// determine whether the right to use the console exists
String user = new String (console. readLine ("Enter username:"); // read the entire line of Characters
String pwd = new String (console. readPassword ("Enter passowrd:"); // read the password, which is not displayed when entered
Console. printf ("Username is:" + user + "\ n"); // display the user name
Console. printf ("Password is:" + pwd + "\ n"); // display the Password
} Else {
System. out. println ("Console is unavailable."); // you are prompted that you do not have the Console permission.
}
For more details, see: · http: // download.oracle.com/javase/6/docs/api/java/io/lele.html
In Bash
Read from standard input
Use the read command to read strings to variables. However, if there is a backslash, it will escape. \ Represents a \ number, \ <newline> represents the continued row, \ <char> represents the <char> itself.
Format: read VAR
Format: 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 a line of text, but cancel the escape Function of the backslash.
Format: read-r VAR
Format: read-p <prompt>-r VAR
Man bash: read writes
-R Backslash does not act as an escape character. The backslash is considered to be part of
Line. In particle, 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 ~] #
About read-p
This parameter is used to display prompt information before reading the variable. If the input is redirected, It is not displayed, for example, the redirection is read from the file.
That is to say, read-p prompt var is not the same as echo PROMPT; read VAR combination.
Man bash: read writes
-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 Password (the entered characters are not displayed)
Format: read-s PASSWORD
Format: read-p <prompt>-s PASSWORD
Man bash: read writes
-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
Reads a specified number of characters
Format: read-n <nchars> VAR
Format: read-p <prompt>-n <nchars> VAR
Man bash: read writes
-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 within the specified time
Format: read-t <seconds> VAR
Format: read-p <prompt>-t <seconds> VAR
Man bash: read writes
-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 from File
Format: read VAR <file.txt
For read commands, you can specify the-r parameter to avoid escape.
Format: read-r VAR <file.txt
Error: cat file.txt | read VAR (here the read command will be executed in the child process, and the child process cannot change the variables of the parent process)
[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 ~] #
The preceding redirection is directly input to the read command. Only one row in the input file can be read.
If you want to read the entire file, you 'd better write it in a code block.
Advanced Bash-Scripting Guide: Chapter 3. Special Characters {} wrote
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 abve, a code block enclosed by {braces} will not normally launch a subshell.
{Read LINE1; read LINE2; read LINE3;} <input.txt
Note that each read command must end with a semicolon.
[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 ~] #
\ In the above LINE2 is not read, because when the-r parameter is not added, the read will be escaped.
Add the-r parameter for testing.
{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 ~] #
Read from Here Document
Error: read VAR <EOF
Some string
EOF
Correct: {read VAR ;}< <EOF
Some string
EOF
Q: Why can I read it only when it is written in a code block?
[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 ~] #
Read from Here String
Read VAR <"some string"
Read VAR <"$ STR"
[Root @ web ~] # Read VAR <"some string"
[Root @ web ~] # Echo $ VAR
Some string
[Root @ web ~] #
Supplement: questions about cat input.txt | read VAR
First, let's talk about the pipeline line: the commands on both sides of the pipeline line. The output of the previous command (standard output) will be used as the input of the next command (standard input ). Commands on both sides are executed in sub-processes and each has an independent address space. The sub-process inherits (copies) All the environment variables of the parent process. Subshell is a special sub-process that inherits (copies) all the variables of the parent Shell. For non-Sub-Shell processes (External commands ), only environment variables are inherited (copied ). However, once a sub-process is started, all its variables are only valid in its process space, and its modifications to the variables are modifications to the variables within its own scope, it does not affect variables with the same name as the parent process. After a child process is started, its parent process changes the parent process variable, and the variables with the same name as the child process have no impact.
Advanced Bash-Scripting Guide: Chapter 3. Special Characters | write
|
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
The output of cat input.txt above will be used as the input of read VAR.
[Root @ web ~] # Cat input.txt | {read VAR; echo $ VAR ;}
Some text here
We can see that the sub-Shell behind the MPs queue does read the value.
[Root @ web ~] # Echo $ VAR
But the same variables in the parent Shell will not change.
[Root @ web ~] #
Author: "Bash @ Linux"