Meta-Character and regexp

36 篇文章 0 订阅
26 篇文章 0 订阅

Regular expressions use meta-characters. The shells also have meta-characters.Meta-characters are simply characters that have a special meaning.The problem occurs when you want to use a regular expression in a shell script. Will the shell do something special with the character?Or will it be passed unchanged to the program?The "$" character is a good example.It could be the beginning of a variable name, or it could be part of a regular expression.If you need a regular expression, you must know if any of the characters of the expression are meta-characters, and must know the right way to quote that character, so that it is passed to the program without being modified by the shell.

Here is a chart of the meta-characters the Bourne and C shell know about.I have also included several combinations of characters just to make this table more complete. There is a lot of detail on this chart.

CharacterWhereMeaning
<RETURN>csh, shExecute command
#csh, sh, ASCII filesStart a comment
<SPACE>csh, shArgument separator
`csh, shCommand substitution
"csh, shWeak Quotes
'csh, shStrong Quotes
\csh, shSingle Character Quote
variablesh, cshVariable
variablecsh, shSame as variable
|csh, shPipe character
^shPipe Character
&csh, shRun program in background
?csh, shMatch one character
*csh, shMatch any number of characters
;csh, shCommand separator
;;shEnd of Case statement
~cshHome Directory
~usercshUser's Home Directory
!cshHistory of Commands
-ProgramsStart of optional argument
$#csh, shNumber of arguments to script
$*csh, shArguments to script
$@shOriginal arguments to script
$-shFlags passed to shell
$?shStatus of previous command
$$shProcess identification number
$!shPID of last background job
&&shShort-circuit AND
||shShort-circuit OR
.csh, shTyp. filename extension
.shSource a file and execute as command
:shNothing command
:shSeparates Values in environment variables
:cshVariable modifier
CharacterWhereMeaning
[ ]csh, shMatch range of characters
[ ]shTest
%jobcshIdentifies job Number
(cmd;cmd)csh. shRuns cmd;cmd as a sub-shell
{ }cshIn-line expansions
{cmd;cmd }shLike (cmd;cmd ) without a subshell
>filecsh, shStandard output
>>filecsh, shAppend to standard output
<filecsh, shStandard Input
<<wordcsh, shRead until word, substitute variables
<<\wordcsh, shRead until word, no substitution
<<-wordshRead until word, ignoring TABS
>>!filecshAppend to file, ignore error if not there
>!filecshOutput to new file, ignore error if not there
>&filecshSend standard & error output to file
<&digitshSwitch Standard Input to file
<&-shClose Standard Input
>&digitshSwitch Standard Output to file
>&-shClose Standard Output
digit1<&digit2shConnect digit2 to digit1
digit<&-shClose file digit
digit2>&digit1shConnect digit2 to digit1
digit>&-shClose file digit


这两天我一直纠结关于正则表达式在emacs,grep,find中何时该加引号,何时不该加引号的问题。虽然我前面的两篇博客“浅谈lisp与shell中转义字符和正则表达”也基本上理清了,因为本身shell/emacs会有一些特殊字符我们称为Meta-Character,而正则表达式也是利用Meta-Character实现的;所以说这个时候就会有一些情况,两个字符在双方都是Meta-character,因为我们既然是正则表达的话,肯定是传给正则表达它所想要的字符形式,但是如果你在shell这边穿过去的时候,它的形式变了。比如$test,你传给正则的不是形式$test而是它的值。所以这就需要进行引号引用或转义了。

唉,我感觉我前两篇写的核心思想,被这个哥们上面的几句话,给结束了;太受挫了。悲了个剧!!!!最后看到它的简介以后心里平衡了,原来是出过书的大牛。。。

Meta-Character in a Unix command that the shell interprets rather than passing to the command也就是说shell碰见下面的这些符号的话,不会说把它传给Command,而是自己对它进行解析。

比如下面[] 和?经过echo值仍旧是[et]?,所以我们就可以安心的传给正则表达式函数了,而不需要加引号。具体是否变化可以通过echo来检验


UNIX SHELL Quote Tutorial

The Grymoire - home for UNIX wizards

浅谈lisp与shell中转义字符和正则表达 (一)

浅谈lisp与shell中转义字符和正则表达 (二)







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值