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

2014-11-24 02:40:31 · 作者: · 浏览: 4
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 < $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

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 < > $S
> EOF
10
[root@jfht tmp]#

格式化输出(printf)
printf "%8s" "$S"
类似C语言的格式化输出,此处不深入探讨。

作者“Bash @ Linux”