Bash字符串处理(与Java对照) - 4.字符串输出(一)

2014-11-24 02:40:31 · 作者: · 浏览: 2

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
\` 反引号
\ 就是\跟上换行,那么换行的作用不再有,只起到续行的作用。
\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 ~]#