欢迎光临
我们一直在努力

用subshel??l和子字符串的Bash替换

本站教程收集整理的这篇文章主要介绍了用subshel??l和子字符串的Bash替换,本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。

给出了一个有创意的例子

FOO="/foo/bar/baz"

这个工程(在bash中)

BAR=$(bas@R_616_8371@ $FOO) # result is BAR="baz"
BAZ=${BAR:0:1}       # result is BAZ="b"

这不是

BAZ=${$(bas@R_616_8371@ $FOO):0:1} # result is bad substitution

我的问题是哪个规则导致这个[subsHel??l替代]错误地评估?如果有的话,在1跳中做什么是正确的方法?

首先,请注意,当你这样说:

BAR=$(bas@R_616_8371@ $FOO) # result is BAR="baz"
BAZ=${BAR:0:1}       # result is BAZ="b"

BAZ的构造中的第一个位是BAR,而不是要采用第一个字符的值.所以即使bash允许变量名包含任意字符,你的第二个表达式的结果也不会是你想要的.

但是,关于阻止这一点的规则,请允许我从bash手册页引用:

DEFinitioNS
       The following deFinitions are used throughout the rest  of  this  docu‐
       ment.
       blank  A space or tab.
       word   A  sequence  of  characters  considered  as a single unit by the
              sHell.  Also kNown as a token.
       name   A word consisTing only of  alphanumeric  characters  and  under‐
              scores,and beginning with an alphabetic character or an under‐
              score.  Also referred to as an identifier.

然后稍后

ParaMETERS
       A parameter is an entity that stores values.  It can be a name,a  num‐
       ber,or one of the special characters listed below under Special Param‐
       eters.  A variable is a parameter denoted by a name.  A variable has  a
       value  and  zero or more attributes.  Attributes are assigned using the
       declare builTin command (see declare below in SHelL BUILTin COMMANDS).

之后当它定义你所要求的语法时:

${parameter:offset:length}
          SubString Expansion.  Expands to  up  to  length  characters  of
          parameter  starTing  at  the  character specified by offset.

因此,在联机帮助页中阐述的规则表示${foo:x:y}构造必须具有参数作为第一部分,并且参数只能是名称,数字或少数特殊参数字符之一. $(bas@R_616_8371@ $FOO)不是参数允许的可能vps云服务器性之一.

对于在一个作业中执行此操作的方式,请使用管道到其他响应中提到的其他命令.

本站总结

以上是本站教程为你收集整理的用subshel??l和子字符串的Bash替换全部内容,希望文章能够帮你解决用subshel??l和子字符串的Bash替换所遇到的程序开发问题。

如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。