Bash字符串處理(與Java對照) – 4.字符串輸出
In Java
輸出到標準輸出設備(控制臺、屏幕)
System.out.println(s);
輸出到標準錯誤設備(控制臺、屏幕)
System.err.println(s);
輸出到文件
PrintWriter outputStream = new PrintWriter(new FileWriter("output_file.txt"));
try {
outputStream.println(s);
} finally { // 別忘記將輸出流關閉,否則會造成資源泄漏
outputStream.close();
}
Line-Oriented I/O (來自 http://download.oracle.com/javase/tutorial/essential/io/charstreams.html)
Java代碼
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
關於echo命令,也可以參考“我使用過的Linux命令之echo – 顯示文本、打印信息 ”。
輸出字符串常量
示例:echo Hello
示例: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]#
輸出變量
格式1:echo $VAR
格式2:echo ${VAR}
上面的格式,如果變量VAR保存的字符串中包含空格、換行,那麼這些空格、跳格、換行將會被壓縮掉。
格式3:echo "$VAR"
格式4:echo "${VAR}"
註意,不能用單引號來引用。
[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]#
註意:echo在輸出信息的時候會自動加上換行,
如果不需要換行,加上 -n參數即可
man echo 寫道
-n do not output the trailing newline
[root@jfht tmp]# echo "Hello"
Hello
[root@jfht tmp]# echo -n "Hello"
Hello[root@jfht tmp]#
echo -e 命令參數中的轉義字符
man echo 寫道
-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.
\" 雙引號 gives the quote its literal meaning
\$ 美元符 gives the dollar sign its literal meaning (variable name following \$ will not be referenced)
\\ 反斜杠、轉義符本身 gives the backslash its literal meaning
\` 反引號
\<newline> 就是\跟上換行,那麼換行的作用不再有,隻起到續行的作用。
\n 換行 means newline
\r 回車 means return
\t 制表符,跳格 means tab
\v 豎制表符 means vertical tab
\b 退格 means backspace
\a 警報 means alert (beep or flash)
\0xx 八進制表示的ASCII碼字符 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 ~]#
輸出命令執行結果
格式1:command line
就是直接執行命令,當然可以
格式2:echo `command line`
格式3:echo $(command line)
這兩種格式的執行效果是一樣的,都會把命令輸出的前後空格去掉、中間的空格換行壓縮為一個空格。
格式2:echo "$(command line)"
格式3:echo "`command line`"
這兩種格式的執行效果也是一樣的,並且會保持命令輸出的空格和換行。一般與直接執行命令的輸出一致。
[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]#
問題來瞭:為什麼 echo "`ls`" 和 echo "$(ls)" 的輸出結果與 直接執行 ls 不一致呢?可以看看下面的解釋:
如果標準輸出是終端,那麼是顯示為多列形式的;否則就是一行一列。而對於$(ls)和`ls`,實際上標準輸出已經不是終端瞭。
info ls 寫道
By default, the output is sorted alphabetically, according to the
locale settings in effect.(1) If standard output is a terminal, the
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.
輸出到文件(標準輸出重定向)
覆蓋原來的文件
格式:echo "$S" >output.txt
追加到文件
格式: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]#
輸出到標準錯誤設備
比如用來打印程序的出錯信息
echo "$S" >&2
>&2表示把標準輸出重定向到文件描述符2,而這正是標準錯誤輸出的文件描述符。
[root@jfht tmp]# if ! ls "*"; then echo "error ls" >&2; fi
ls: *: 沒有那個文件或目錄
error ls
[root@jfht tmp]#
上面命令行的意思是說列出文件名為*的文件,如果出錯,就打印錯誤信息。
輸出到別的進程
管道線(|)
示例:echo "$S" | wc -c
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.
Here Document方式
示例:wc -c <<EOF
$S
EOF
Advanced Bash-Scripting Guide: Chapter 19. Here Documents 寫道
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方式
示例:wc -c <<<"$S"
Advanced Bash-Scripting Guide: 19.1. Here Strings 寫道
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]#
格式化輸出(printf)
printf "%8s" "$S"
類似C語言的格式化輸出,此處不深入探討。
作者“Bash @ Linux”