- 浏览: 4763729 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
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 5553Bash字符串处理(与Java对照) - 22.判断字符串是否 ... -
Bash字符串处理(与Java对照) - 21.字符串正则匹配
2011-10-24 09:07 11113Bash字符串处理(与Java对照) - 21.字符串正则匹配 ... -
Bash字符串处理(与Java对照) - 20.查找子串的位置
2011-10-19 09:14 6882Bash字符串处理(与Java对照) - 20.查找子串的位置 ... -
Bash字符串处理(与Java对照) - 19.查找字符的位置
2011-10-18 09:06 5965Bash字符串处理(与Java对照) - 19.查找字符的位置 ... -
Bash字符串处理(与Java对照) - 18.格式化字符串
2011-10-17 09:18 5064Bash字符串处理(与Java对照) - 18.格式化字符串 ... -
Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
2011-10-09 08:58 7044Bash字符串处理(与Java对照) - 17.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
2011-10-08 09:17 8463Bash字符串处理(与Java对照) - 16.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 15.计算子串出现的次数
2011-09-28 09:37 3476Bash字符串处理(与Java对照) - 15.计算子串出现的 ... -
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
2011-09-27 13:22 8415Bash字符串处理(与Java对照) - 14.判断是否包含另 ... -
Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
2011-09-26 09:19 5268Bash字符串处理(与Java对照) - 13.字符串数组连接 ... -
Bash字符串处理(与Java对照) - 12.字符串连接
2011-09-23 09:08 6392Bash字符串处理(与Java对照) - 12.字符串连接 ... -
Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)
2011-09-21 09:31 5768Bash字符串处理(与Java对照) - 11.比较两个字符串 ... -
Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)
2011-09-20 09:16 6997Bash字符串处理(与Java对照) - 10.判断两个字符串 ... -
Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
2011-09-19 09:13 3782Bash字符串处理(与Java对照) - 9.获取字符串指定位 ... -
Bash字符串处理(与Java对照) - 8.计算字符串长度
2011-09-16 09:20 5740Bash字符串处理(与Java对照) - 8.计算字符串长度 ... -
Bash字符串处理(与Java对照) - 7.字符串与默认值
2011-09-15 09:20 4018Bash字符串处理(与Java对照) - 7.字符串与默认值 ... -
Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
2011-09-14 09:20 7286Bash字符串处理(与Java对照) - 6.判断字符串是否为 ... -
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
2011-09-13 09:28 3996Bash字符串处理(与Java对照) - 5.字符串输入(读取 ... -
Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
2011-09-07 09:29 6953Bash字符串处理(与Java ... -
Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)
2011-09-06 09:18 6216Bash字符串处理(与Java ...
相关推荐
对于 `decimal` 数据类型,C语言中没有直接对应的类型,可以通过转换为字符串或浮点类型来处理。 #### 3.3 如何生成可执行的应用程序 开发流程包括以下步骤: 1. **创建源文件**:编写包含SQL语句的C源文件(例如...
chromedriver-win64-136.0.7059.0.zip
python学习一些项目和资源
python学习资源
python学习资源
python学习教程
python学习教程
【毕业设计】java-springboot+vue会议管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
内有各个系统的版本全了
分数阶模型辨识,分数阶模型辨识
大数据基于python的电影天堂数据可视化(源码+配套文档) 系统功能: 登录 、首页 、电影数据管理 、我的信息 关键技术:Python、Django、Mysql、Hadoop、Scrapy、Vue、B/S 技术支持:已测试可正常运行,调试问题可联系客服有偿解决。 更多项目:3000+优质源码,支持【定制】、修改、部署、讲解和文档。
【毕业设计】java-springboot+vue疾病防控综合系统的设计与实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计】java-springboot-vue家具销售电商平台实现源码(完整前后端+mysql+说明文档+LunW).zip
134dfffffffffffffffffffffffffffffff
代码说明: 设置结束时间:通过new Date().getTime()获取当前时间戳,并加上10分钟的毫秒数(10 * 60 * 1000),得到倒计时的结束时间。 更新倒计时:updateCountdown函数计算当前时间与结束时间的差值,并将其转换为分钟和秒数。 显示倒计时:通过console.log输出剩余时间,格式为“剩余时间:X分Y秒”。 停止倒计时:当剩余时间小于或等于0时,清除定时器并输出“时间到!”。 定时器:使用setInterval每秒调用一次updateCountdown函数,实现倒计时的动态更新。 扩展说明: 应用场景:倒计时功能常用于限时抢购、考试计时、活动倒计时等场景。 优化建议:可以将倒计时显示在网页的某个元素中,而不是控制台。例如,使用document.getElementById获取DOM元素并更新其内容。 兼容性:该代码在现代浏览器中均可运行,如果需要兼容旧版浏览器,可以使用var代替const和let。 扩展功能:可以添加声音提示、动画效果等,提升用户体验。
该项目是一个大学生校园兼职平台。该平台使用Java语言开发后台业务逻辑,运用了SpringMVC+Spring+MyBatis框架进行搭建,前台使用jQuery、layUI框架,数据库服务器采用MySQL5.6+对数据进行持久化。其主要功能有:兼职招聘、论坛交流、在线聊天、个人中心、信箱留言、登录注册等功能。
图解AUTOSAR-CP-CommunicationStackTypes逻辑图打包
解释程序的逻辑和变量等等
python学习一些项目和资源
最近在基于大型语言模型(LLM)的多智能体系统(MAS)方面的发展展示了其在处理复杂决策任务方面的显著潜力。然而,现有的框架不可避免地依赖于串行执行范式,即智能体必须完成顺序的LLM规划后才能采取行动。这一基本限制严重影响了实时响应和适应能力,而在动态环境中这些能力至关重要。本文提出了一种新的并行化规划-行动框架,用于基于LLM的MAS,该框架具有可中断执行的双线程架构,支持并发规划和行动。具体而言,我们的框架包含两个核心线程:(1) 由集中式内存系统驱动的规划线程,保持环境状态同步和智能体通信以支持动态决策;以及 (2) 配备全面技能库的行动线程,通过递归分解实现自动化任务执行。在具有挑战性的《我的世界》实验中证明了所提框架的有效性。