Bash string processing (compared with Java)-4. String output

Source: Internet
Author: User
Tags control characters

Bash string processing (compared with Java)-4. String output

In Java
Output to standard output devices (console, screen)
System. out. println (s );
 
Output to standard error devices (console, screen)
System. err. println (s );
 
Output to file
PrintWriter outputStream = new PrintWriter (new FileWriter ("output_file.txt "));
Try {
OutputStream. println (s );
} Finally {// do not forget to close the output stream; otherwise, resource leakage may occur.
OutputStream. close ();
}
 
Line-Oriented I/O from http://download.oracle.com/javase/tutorial/essential/io/charstreams.html)
Java code
Import java. io. FileReader;
Import java. io. FileWriter;
Import java. io. BufferedReader;
Import java. io. PrintWriter;
Import java. io. IOException;
 
Public class CopyLines {
Public static void main (String [] args) throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
 
Try {
InputStream =
New BufferedReader (new FileReader ("xanadu.txt "));
OutputStream =
New PrintWriter (new FileWriter ("characteroutput.txt "));
 
String l;
While (l = inputStream. readLine ())! = Null ){
OutputStream. println (l );
}
} Finally {
If (inputStream! = Null ){
InputStream. close ();
}
If (outputStream! = Null ){
OutputStream. close ();
}
}
}
}
 
In Bash
Echo
For details about the echo command, refer to "echo-display text and print information of Linux commands I have used ".
 
Output String constant
Example: echo Hello
Example: echo "Hello"
 
[Root @ jfht tmp] # echo Hello
Hello
[Root @ jfht tmp] # echo "Hello"
Hello
[Root @ jfht tmp] # echo Hello World
Hello World
[Root @ jfht tmp] # echo "Hello World"
Hello World
[Root @ jfht tmp] #
 
 
Output variable
Format 1: echo $ VAR
Format 2: echo $ {VAR}
In the preceding format, if the string saved by the variable VAR contains spaces and line breaks, these spaces, hop cells, and line breaks will be compressed.
Format 3: echo "$ VAR"
Format 4: echo "$ {VAR }"
Note: you cannot use single quotation marks to reference the data.
 
[Root @ jfht tmp] # VAR = "Hello World"
[Root @ jfht tmp] # echo $ VAR
Hello World
[Root @ jfht tmp] # echo $ {VAR}
Hello World
[Root @ jfht tmp] # echo "$ VAR"
Hello World
[Root @ jfht tmp] # echo "$ {VAR }"
Hello World
[Root @ jfht tmp] # echo '$ VAR'
$ VAR
[Root @ jfht tmp] # echo $ '$ VAR'
$ VAR
[Root @ jfht tmp] #
 
 
Note: echo automatically adds a line feed when outputting information,
If you do not need a line feed, add the-n parameter.
Man echo wrote:
-N do not output the trailing newline
 
[Root @ jfht tmp] # echo "Hello"
Hello
[Root @ jfht tmp] # echo-n "Hello"
Hello [root @ jfht tmp] #
 
Escape characters in echo-e Command Parameters
Man echo wrote:
-E enable interpretation of backslash escapes

If-e is in effect, the following sequences are recognized:

\ 0NNN the character whose ASCII code is NNN (octal)
\ Backslash
\ A alert (BEL)
\ B backspace
\ C suppress trailing newline
\ F form feed
\ N new line
\ R carriage return
\ T horizontal tab
\ V vertical tab

NOTE: your shell may have its own version of echo, which usually supersedes the version described here. Please
Refer to your shell's documentation for details about the options it supports.
 
\ "Double quotation marks gives the quote its literal meaning
\ $ Dollar sign gives the dollar sign its literal meaning (variable name following \ $ will not be referenced)
\ Backslash, escape character itself gives the backslash its literal meaning
\ 'Reverse quotation marks
\ <Newline> is \ to keep up with the line feed, so the line feed is no longer used, only to continue the line.
\ N newline means newline
\ R press ENTER means return
\ T tab, hop means tab
\ V vertical tab means vertical tab
\ B backspace
\ A alarm means alert (beep or flash)
\ 0xx octal ASCII character translates to the octal ASCII equivalent of 0nn, where nn is a string of digits
 
[Root @ jfht ~] # Echo-e '\ N'


[Root @ jfht ~] # Echo-e 'a \ tb'
A B
[Root @ jfht ~] #
 
 
Output command execution result
Format 1: command line
Is to directly execute the command, of course, can
Format 2: echo 'COMMAND Line'
Format 3: echo $ (command line)
The execution results of these two formats are the same. Both of them will remove the leading and trailing spaces of the command output, and compress the trailing and trailing spaces into a space line break.
Format 2: echo "$ (command line )"
Format 3: echo "'COMMAND line '"
The execution results of these two formats are the same, and the space and line feed of the command output are maintained. Generally, the output is the same as that of the command directly executed.
 
 
[Root @ jfht tmp] # ls
Ct08 ct08.min.tar.gz ls0.txt ls1.txt ls2.txt
[Root @ jfht tmp] # echo 'LS'
Ct08 ct08.min.tar.gz ls0.txt ls1.txt ls2.txt
[Root @ jfht tmp] # echo $ (ls)
Ct08 ct08.min.tar.gz ls0.txt ls1.txt ls2.txt
[Root @ jfht tmp] # echo "'LS '"
Ct08
Ct08.min.tar.gz
Ls0.txt
Ls1.txt
Ls2.txt
[Root @ jfht tmp] # echo "$ (ls )"
Ct08
Ct08.min.tar.gz
Ls0.txt
Ls1.txt
Ls2.txt
[Root @ jfht tmp] #
Question: Why are the output results of echo "'LS'" and echo "$ (ls)" different from those of direct ls execution? Let's take a look at the following explanation:
If the standard output is a terminal, it is displayed as multiple columns; otherwise, it is a row or column. For $ (ls) and 'Ls', the standard output is no longer a terminal.
Info ls writes
By default, the output is sorted alphabetically, according to
Locale settings in effect. (1) If standard output is a terminal,
Output is in columns (sorted vertically) and control characters are
Output as question marks; otherwise, the output is listed one per line
And control characters are output as-is.
 
 
Output to file (standard output redirection)
Overwrite the original file
Format: echo "$ S"> output.txt
 
Append to file
Format: echo "$ S"> output.txt
 
[Root @ jfht tmp] # S = 123456789
[Root @ jfht tmp] # echo "$ S"> output.txt
[Root @ jfht tmp] # cat output.txt
123456789
[Root @ jfht tmp] # echo "$ S"> output.txt
[Root @ jfht tmp] # cat output.txt
123456789
[Root @ jfht tmp] # echo "$ S"> output.txt
[Root @ jfht tmp] # cat output.txt
123456789
123456789
[Root @ jfht tmp] #
 
Output to standard error device
For example, to print program error information
Echo "$ S"> & 2
> & 2 indicates that the standard output is redirected to file descriptor 2, which is the file descriptor output by the standard error.
 
[Root @ jfht tmp] # if! Ls "*"; then echo "error ls"> & 2; fi
Ls: *: the file or directory does not exist.
Error ls
[Root @ jfht tmp] #
The command line above indicates listing objects with the file name *. If an error occurs, the error message is printed.
 
Output to other processes
Pipe Line (|)
Example: echo "$ S" | wc-c
Advanced Bash-Scripting Guide: Chapter 3. Special Characters wrote
|

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.
 
Here Document Method
Example: wc-c <EOF
$ S
EOF
Advanced Bash-Scripting Guide: Chapter 19. Here Documents ents writes
A here document is a special-purpose code block. it uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.

COMMAND <InputComesFromHERE
...
...
...
InputComesFromHERE

A here document supports parameter and command substitution. It is therefore possible to pass different parameters to the body of the here document, changing its output accordingly.
 
 
Here String
Example: wc-c <"$ S"
Advanced Bash-Scripting Guide: 19.1. Here Strings writes
A here string can be considered as a stripped-down form of a here document.
It consists of nothing more than COMMAND <$ WORD,
Where $ WORD is expanded and fed to the stdin of COMMAND.
 
 
[Root @ jfht tmp] # S = 123456789
[Root @ jfht tmp] # echo "$ S" | wc-c
10
[Root @ jfht tmp] # wc-c <"$ S"
10
[Root @ jfht tmp] # wc-c <EOF
> $ S
> EOF
10
[Root @ jfht tmp] #
 
Format the output (printf)
Printf "% 8 s" "$ S"
Similar to the formatted output in C language, we will not discuss it in detail here.

Author: "Bash @ Linux"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.