- 浏览: 4733559 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
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对照) - 2.字符串的表示方式(字符串常量)
In Java
你懂的!
使用单引号表示字符常量:'c'
使用双引号表示字符串常量:"hello world"
Java字符转义
\u???? 四位十进制Unicode字符
\??? 三位八进制字符
\n 换行(\u000a)(Insert a newline in the text at this point.)
\t 水平制表符(\u0009)(Insert a tab in the text at this point.)
\b 退格(\u0008)(Insert a backspace in the text at this point.)
\r 回车(\u000d)(Insert a carriage return in the text at this point.)
\f 换页(\u000c)(Insert a formfeed in the text at this point.)
\' 单引号(\u0027)(Insert a single quote character in the text at this point.)
\" 双引号(\u0022)(Insert a double quote character in the text at this point.)
\\ 反斜杠(\u005c)(Insert a backslash character in the text at this point.)
注:网上有很多资料都不一定正确,还是看Oracle网站上关于Characters的内容为好:
http://download.oracle.com/javase/tutorial/java/data/characters.html
In Bash
字符串的用途
在Bash中无处不是字符串,它可以
(1)作为命令;
(2)作为命令行参数;
(3)作为变量的值;
(4)作为重定向的文件名称;
(5)作为输入字符串(here string, here document);
(6)作为命令行的输出。
字符串表示方式概述
在Bash中,字符串常量有多种表示方式
(1)不使用任何引号,比如 hello
(2)使用一对单引号括起来,比如 'hello'
(3)使用一对双引号括起来,比如 "hello"
(4)使用美元符加上一对单引号括起来,比如 $'hello'
如果字符串中不包含特殊字符,那么上述几种表示方式是等效的。
友情提示:字符串的这几种表示方式可以任意组合,比如 h'e'"l"$'lo' 与 hello是等效的。
[root@jfht ~]# echo hello
hello
[root@jfht ~]# echo 'hello'
hello
[root@jfht ~]# echo "hello"
hello
[root@jfht ~]# echo $'hello'
hello
[root@jfht ~]# echo h'e'"l"$'lo'
hello
[root@jfht ~]#
关于字符串表示的问题
在进一步对字符串的表示方式了解之前,请先回答下面的问题:
问题1:字符串中包含空格,应该怎么表示?
问题2:字符串中包含制表符,应该怎么表示?
问题3:字符串中包含单引号,应该怎么表示?
问题4:字符串中包含双引号,应该怎么表示?
问题5:字符串中包含$,应该怎么表示?
问题6:字符串中包含#,应该怎么表示?
问题7:字符串中包含换行,即有多行,应该怎么表示?
问题8:字符串中包含反斜杠,应该怎么表示?
问题9:字符串中包含感叹号,应该怎么表示?
无引号(one of quoting mechanisms: the escape character)
在没有用引号括起来时,所有的特殊字符都会产生特殊的作用,如果想让它们失去特殊作用,就需要用转义符(\)进行转义。
literal value of the next character that follows, with the exception of
<newline>. If a \<newline> pair appears, and the backslash is not
itself quoted, the \<newline> is treated as a line continuation (that
is, it is removed from the input stream and effectively ignored).
空格是用来分隔命令与参数、参数与参数、变量赋值和命令的,所以如果字符串中有空格时,需要对空格进行转义。
[root@jfht ~]# STR=hello world
-bash: world: command not found
[root@jfht ~]# STR=hello\ world
换行符是命令行之间的分隔符的一种(另外一种是分号),如果字符串比较长,就可以对换行符进行转义,变成续行。
[root@jfht ~]# STR=hello\
> world
[root@jfht ~]#
在没有任何引号括起来时,\跟上字符,那么所跟的字符将失去特殊含义。比如 管道线(|)。
[root@jfht ~]# STR=hello|world
-bash: world: command not found
[root@jfht ~]# STR=hello\|world
[root@jfht ~]#
单引号(single quote,full quoting)
在单引号中,所有的特殊字符豆浆失去其特殊含义。这样也就导致在单引号中无法表示单引号字符本身。
each character within the quotes. A single quote may not occur between
single quotes, even when preceded by a backslash.
[root@jfht ~]# echo '$'
$
[root@jfht ~]# echo '`'
`
[root@jfht ~]# echo '\'
\
[root@jfht ~]# echo '!'
!
[root@jfht ~]# echo 'begin of string
> some strings
> end of string'
begin of string
some strings
end of string
[root@jfht ~]#
下面展示一下单引号表示的字符串在脚本中的表现。
Bash脚本:test_single_quote.sh
#!/bin/sh echo '$' echo '`' echo '\' echo '!' echo 'start of string some strings end of string'
[root@jfht ~]# chmod +x ./test_single_quote.sh
[root@jfht ~]# ./test_single_quote.sh
$
`
\
!
start of string
some strings
end of string
[root@jfht ~]#
双引号(double quote,partial quoting)
在双引号中,大部分特殊字符失去了其特殊含义,比如 注释(#)、命令分隔符(;)、管道线(|)、通配符(?*)等。
[root@jfht work191]# echo *
cgen ct08 hycu2 hyfc2 jlib keyc mdbc msgc sms web_spider xqt_admin
xqt_demo xqt_server zjlt_mhr_files zjlt_mhr_server zjlt_mhr_user
zjlt_mhr_web
[root@jfht work191]# echo "*"
*
[root@jfht work191]# echo #
[root@jfht work191]# echo "#"
#
[root@jfht work191]#
all characters within the quotes, with the exception of $, `, and \.
在双引号中,特殊字符包括:美元符($)、反引号(`)、转义符(\)、感叹号(!),其他的特殊字符就不用管了。(为啥在此处写上“感叹号(!)”呢,请看后文分解)
quotes.
在双引号中,美元符($)可以引用变量,将被替换为变量的值,比如
"$VAR" 被替换为变量VAR的值,但"$VAR123"将被替换为变量VAR123的值,所以最好用大括号把变量括起来,避免意想不到的错误出现;
"${VAR}" 被替换为变量VAR的值,"${VAR}123"将被替换为变量VAR的值再跟上123;
"${VAR:-DEFAULT}" 当变量VAR没有定义或者为空时,替换为DEFAULT,否则替换为变量VAR的值;
"$(command line)" 相当于 "`command line`",将被替换为命令行执行的标准输出信息
变量VAR没有定义。
[root@jfht ~]# echo "$VAR"
[root@jfht ~]# echo "${VAR}"
[root@jfht ~]# echo "$VAR123"
[root@jfht ~]# echo "${VAR}123"
123
[root@jfht ~]# echo "${VAR:-DEFAULT}"
DEFAULT
现在定义VAR变量。
[root@jfht ~]# VAR="Hello World"
[root@jfht ~]# echo "$VAR"
Hello World
[root@jfht ~]# echo "${VAR}"
Hello World
[root@jfht ~]# echo "$VAR123"
[root@jfht ~]# echo "${VAR}123"
Hello World123
[root@jfht ~]# echo "${VAR:-DEFAULT}"
Hello World
现在测试一下命令执行输出结果。
[root@jfht ~]# echo "$(pwd)"
/root
[root@jfht ~]#
在双引号中,反引号(`)将被替换为命令行执行的标准输出信息
"`ls`" 相当于 "$(ls)",将被替换为ls命令的执行结果,即列出当前目录的文件名;
"`date +%F`"相当于"$(date +%F)",将被替换为date +%F的执行结果,即当天的日期,比如2011-09-01
[root@jfht ~]# echo "`pwd`"
/root
[root@jfht ~]# echo "`date +%F`"
2011-09-02
[root@jfht ~]# echo "$(date +%F)"
2011-09-02
[root@jfht ~]#
by one of the following characters: $, `, ", \, or <newline>. A double
quote may be quoted within double quotes by preceding it with a back-
slash.
在双引号中,转义符(\)是为了方便创建包含特殊字符的字符串,特殊字符的前面需要加上\进行转义
\" 双引号 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> 就是\跟上换行,那么换行的作用不再有,只起到续行的作用。
[root@jfht ~]# echo ""
[root@jfht ~]# echo "hello
> world"
hello
world
[root@jfht ~]#
[root@jfht ~]# echo "hello world\""
hello world"
[root@jfht ~]# echo "hello world\$"
hello world$
[root@jfht ~]# echo "hello world\\"
hello world\
[root@jfht ~]# echo "hello world\`"
hello world`
[root@jfht ~]# echo "hello world\
> yes"
hello worldyes
[root@jfht ~]#
在双引号中,感叹号(!)的含义根据使用的场合有所不同,在命令行环境,它将被解释为一个历史命令,而在脚本中,则不会有特殊含义。
在命令行环境,感叹号(!)称之为“历史扩展字符(the history expansion character)”。
[root@jfht ~]# pwd
/root
[root@jfht ~]# echo "!"
-bash: !: event not found
[root@jfht ~]# echo "!pwd"
echo "pwd"
pwd
[root@jfht ~]#
在脚本中使用感叹号,将不会进行历史扩展。
Bash脚本:test_exclamation_mark.sh
#!/bin/sh echo "!" pwd echo "!pwd"
[root@smsgw root]# ./test_exclamation_mark.sh
!
/root
!pwd
[root@smsgw root]#
$' ... '(ANSI-C Quoting)
前面讲到,包围在单引号之内的字符都不会有特殊含义,所以单引号本身并不能在一对单引号中出现。但是在前面加上$之后,就可以使用\进行转义了,\的转义含义与C语言中的相同。
Quoted string expansion. This construct expands single or multiple escaped octal or hex values into ASCII or Unicode characters.
ters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as fol-
lows:
\a alert (bell)
\b backspace
\e an escape character (not ANSI C)
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\nnn the eight-bit character whose value is the octal value nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\cx a control-x character
The expanded result is single-quoted, as if the dollar sign had not been present.
叮当一声。
[root@jfht ~]# echo $'\a'
叮当三声。
[root@jfht ~]# echo $'\a\a\a'
单引号。
[root@jfht ~]# echo $'\''
'
八进制表示的ASCII字符。
[root@jfht ~]# echo $'\101\102\103\010'
ABC
[root@jfht ~]#
[root@jfht ~]#
$"..."(Locale-Specific Translation)
在双引号之前加上$,这部分内容还未能很好的理解,就不说了。望高人指点一二。
string to be translated according to the current locale. If the cur-
rent locale is C or POSIX, the dollar sign is ignored. If the string
is translated and replaced, the replacement is double-quoted.
问题解答
问题1:字符串中包含空格,应该怎么表示?
问题2:字符串中包含制表符,应该怎么表示?
问题3:字符串中包含单引号,应该怎么表示?
问题4:字符串中包含双引号,应该怎么表示?
问题5:字符串中包含$,应该怎么表示?
问题6:字符串中包含#,应该怎么表示?
问题7:字符串中包含换行,即有多行,应该怎么表示?
问题8:字符串中包含反斜杠,应该怎么表示?
问题9:字符串中包含感叹号,应该怎么表示?
感谢大家怀着极大的耐心看完了上述内容,上面9道题的答案在不久之后将公布,有兴趣的请直接跟帖回复。
本文链接:http://codingstandards.iteye.com/blog/1166282 (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 1.(字符串)变量声明
下节内容:Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
发表评论
-
Bash字符串处理(与Java对照) - 22.判断字符串是否数字串
2011-10-25 09:25 5426Bash字符串处理(与Java对照) - 22.判断字符串是否 ... -
Bash字符串处理(与Java对照) - 21.字符串正则匹配
2011-10-24 09:07 11010Bash字符串处理(与Java对照) - 21.字符串正则匹配 ... -
Bash字符串处理(与Java对照) - 20.查找子串的位置
2011-10-19 09:14 6735Bash字符串处理(与Java对照) - 20.查找子串的位置 ... -
Bash字符串处理(与Java对照) - 19.查找字符的位置
2011-10-18 09:06 5901Bash字符串处理(与Java对照) - 19.查找字符的位置 ... -
Bash字符串处理(与Java对照) - 18.格式化字符串
2011-10-17 09:18 4928Bash字符串处理(与Java对照) - 18.格式化字符串 ... -
Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾
2011-10-09 08:58 6930Bash字符串处理(与Java对照) - 17.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
2011-10-08 09:17 8301Bash字符串处理(与Java对照) - 16.判断是否以另外 ... -
Bash字符串处理(与Java对照) - 15.计算子串出现的次数
2011-09-28 09:37 3369Bash字符串处理(与Java对照) - 15.计算子串出现的 ... -
Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
2011-09-27 13:22 8269Bash字符串处理(与Java对照) - 14.判断是否包含另 ... -
Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)
2011-09-26 09:19 5119Bash字符串处理(与Java对照) - 13.字符串数组连接 ... -
Bash字符串处理(与Java对照) - 12.字符串连接
2011-09-23 09:08 6315Bash字符串处理(与Java对照) - 12.字符串连接 ... -
Bash字符串处理(与Java对照) - 11.比较两个字符串大小(字典顺序、数值比较)
2011-09-21 09:31 5647Bash字符串处理(与Java对照) - 11.比较两个字符串 ... -
Bash字符串处理(与Java对照) - 10.判断两个字符串是否相等(不等)
2011-09-20 09:16 6844Bash字符串处理(与Java对照) - 10.判断两个字符串 ... -
Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
2011-09-19 09:13 3692Bash字符串处理(与Java对照) - 9.获取字符串指定位 ... -
Bash字符串处理(与Java对照) - 8.计算字符串长度
2011-09-16 09:20 5595Bash字符串处理(与Java对照) - 8.计算字符串长度 ... -
Bash字符串处理(与Java对照) - 7.字符串与默认值
2011-09-15 09:20 3895Bash字符串处理(与Java对照) - 7.字符串与默认值 ... -
Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)
2011-09-14 09:20 7158Bash字符串处理(与Java对照) - 6.判断字符串是否为 ... -
Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)
2011-09-13 09:28 3886Bash字符串处理(与Java对照) - 5.字符串输入(读取 ... -
Bash字符串处理(与Java对照) - 4.字符串输出
2011-09-08 09:30 3699Bash字符串处理(与Java对照) - 4.字符串输出 I ... -
Bash字符串处理(与Java对照) - 3.给(字符串)变量赋值
2011-09-07 09:29 6838Bash字符串处理(与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官方离线安装包,亲测可用
离线安装包,测试可用
-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 查看版本出现: 原因是:没有那个文件或目录,找了很久发现需要...
bash-4.1.2-9.el6_2.i686.rpm是centos工具类。
openjdk-18.0.1.1(openjdk-18.0.1.1_linux-x64_bin.tar.gz)适用于Linux x64 系统:
git-bash.exe
修改redhat 7 bash远程执行任意代码漏洞
在Linux下使用sqlplus或者rman时,经常需要调用上次或之前运行过的命令 下载包地址: ... 地址2: ... 1. rpm -ivh rlwrap-0.42-1.el7.x86_64.rpm ...2. 检查包是否安装上 rpm -qa rlwrap ...4. 生效: source .bash_profile
aix 操作系统bash工具bash-3.2-2.aix6.1.ppc.rpm,aix 5及6、7均支持
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
linux 服务器GNU Bash小于版本4.3有操作系统命令注入漏洞,需要对bash升级,下载解压 #tar zxvf bash-4.4.tar.gz #cd bash-4.4 #./configure (如果centos7编译失败,请先安装#yum install gcc) #make #make ...
.git-completion.bash