- 浏览: 4734337 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
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)
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参数即可
[root@jfht tmp]# echo "Hello"
Hello
[root@jfht tmp]# echo -n "Hello"
Hello[root@jfht tmp]#
echo -e 命令参数中的转义字符
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`,实际上标准输出已经不是终端了。
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
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
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"
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语言的格式化输出,此处不深入探讨。将在本系列的第18节详述。
本文链接:http://codingstandards.iteye.com/blog/1168164 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
下节内容:Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
发表评论
-
Bash字符串处理(与Java对照) - 22.判断字符串是否数字串
2011-10-25 09:25 5432Bash字符串处理(与Java对照) - 22.判断字符串是否 ... -
Bash字符串处理(与Java对照) - 21.字符串正则匹配
2011-10-24 09:07 11012Bash字符串处理(与Java对照) - 21.字符串正则匹配 ... -
Bash字符串处理(与Java对照) - 20.查找子串的位置
2011-10-19 09:14 6736Bash字符串处理(与Java对照) - 20.查找子串的位置 ... -
Bash字符串处理(与Java对照) - 19.查找字符的位置
2011-10-18 09:06 5905Bash字符串处理(与Java对照) - 19.查找字符的位置 ... -
Bash字符串处理(与Java对照) - 18.格式化字符串
2011-10-17 09:18 4929Bash字符串处理(与Java对照) - 18.格式化字符串 ... -
Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
2011-10-09 08:58 6936Bash字符串处理(与Java对照) - 17.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
2011-10-08 09:17 8305Bash字符串处理(与Java对照) - 16.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 15.计算子串出现的次数
2011-09-28 09:37 3370Bash字符串处理(与Java对照) - 15.计算子串出现的 ... -
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
2011-09-27 13:22 8272Bash字符串处理(与Java对照) - 14.判断是否包含另 ... -
Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
2011-09-26 09:19 5122Bash字符串处理(与Java对照) - 13.字符串数组连接 ... -
Bash字符串处理(与Java对照) - 12.字符串连接
2011-09-23 09:08 6315Bash字符串处理(与Java对照) - 12.字符串连接 ... -
Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)
2011-09-21 09:31 5654Bash字符串处理(与Java对照) - 11.比较两个字符串 ... -
Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)
2011-09-20 09:16 6847Bash字符串处理(与Java对照) - 10.判断两个字符串 ... -
Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
2011-09-19 09:13 3694Bash字符串处理(与Java对照) - 9.获取字符串指定位 ... -
Bash字符串处理(与Java对照) - 8.计算字符串长度
2011-09-16 09:20 5599Bash字符串处理(与Java对照) - 8.计算字符串长度 ... -
Bash字符串处理(与Java对照) - 7.字符串与默认值
2011-09-15 09:20 3895Bash字符串处理(与Java对照) - 7.字符串与默认值 ... -
Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
2011-09-14 09:20 7161Bash字符串处理(与Java对照) - 6.判断字符串是否为 ... -
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
2011-09-13 09:28 3887Bash字符串处理(与Java对照) - 5.字符串输入(读取 ... -
Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
2011-09-07 09:29 6838Bash字符串处理(与Java ... -
Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)
2011-09-06 09:18 6085Bash字符串处理(与Java ...
相关推荐
bash-3.1-MSYS-1.0.11-snapshot.tar.bz2 ffmpeg 安装用包!
在centos6或rhel6 操作系统安装bash-completion-2.1-6.el7.noarch.rpm后,重启即生效
离线安装包,测试可用
libguestfs-bash-completion-1.40.2-28.module_el8.5.0+821+97472045.noarch(1).rpm官方离线安装包,亲测可用
openjdk-18.0.1.1(openjdk-18.0.1.1_linux-x64_bin.tar.gz)适用于Linux x64 系统:
-bash: /usr/local/jdk/jdk1.8.0_181/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 安装完后 java -version 查看版本出现: 原因是:没有那个文件或目录,找了很久发现需要...
git-bash.exe
修改redhat 7 bash远程执行任意代码漏洞
bash-4.1.2-9.el6_2.i686.rpm是centos工具类。
在Linux下使用sqlplus或者rman时,经常需要调用上次或之前运行过的命令 下载包地址: ... 地址2: ... 1. rpm -ivh rlwrap-0.42-1.el7.x86_64.rpm ...2. 检查包是否安装上 rpm -qa rlwrap ...4. 生效: source .bash_profile
redhat bash漏洞补丁包含文件:bash-4.1.2-15.el6_5.2.i686.rpmbash-4.1.2-15.el6_5.2.x86_64.rpmbash-3.0-27.el4.i386.rpmbash-3.0-27.el4.x86_64.rpmbash-3.2-33.el5_11.4.i386.r... redhat bash漏洞补丁 包含文件...
修复redhat6 bash远程执行任意代码漏洞CVE-2014-6271
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
命令补全 Centos6/7或Rhel6/7 安装 bash-completion-2.1-6.el7.noarch.rpm 后重启即生效
网上有很多bash补丁下载,但都是x86_64架构的,针对i686的下载很少,bash-4.1.2-15.el6_5.2.src.rpm 也很难下载,希望上传的这个bash-4.1.2-15.el6_5.2.i686.rpm对你有用。
检测方法 执行如下命令查看设备是否存在bash安全漏洞。 #env -i X='() { (a)=>\\' bash -c '/dev/stdout echo vulnerable' 2>/dev/null ... 如果以上命令的输出为以下内容则不合规: vulnerable
离线安装包,亲测可用
#tar zxvf bash-4.4.tar.gz #cd bash-4.4 #./configure (如果centos7编译失败,请先安装#yum install gcc) #make #make install 添加环境变量 #echo‘ export PATH=/usr/local/bin:$PATH‘ >>/etc/profile #source...
.git-completion.bash