6

我正在尝试在 bash 脚本中执行一个简单的正则表达式语句,该语句将匹配并替换单词的结尾。以下是我正在尝试做的事情。

wordh > word:’

下面是我正在使用的代码。

#!/bin/bash
STAT=${STAT/h$/:’}

我不熟悉 bash 脚本,我认为它与 bash 有关,$因为它用于标记变量。我试图逃避它,并/在它之后添加另一个。当我删除$它时(不检查单词的结尾)。

4

2 回答 2

4

正则表达式有一点不同。尝试:

STAT=${STAT/%h/:’}

从手册页:

${参数/模式/字符串}

.         The pattern is expanded to produce a pattern just as in pathname
          expansion.   Parameter is expanded and the longest match of pat-
          tern against its value is replaced  with  string.   If  Ipattern
          begins  with /, all matches of pattern are replaced with string.
          Normally only the first match is replaced.   If  pattern  begins
          with  #, it must match at the beginning of the expanded value of
          parameter.  If pattern begins with %, it must match at  the  end
          of  the expanded value of parameter.  If string is null, matches
          of pattern are deleted and the / following pattern may be  omit-
          ted.   If  parameter  is  @  or *, the substitution operation is
          applied to each positional parameter in turn, and the  expansion
          is  the  resultant list.  If parameter is an array variable sub-
          scripted with @ or *, the substitution operation is  applied  to
          each  member  of  the  array  in  turn, and the expansion is the
          resultant list.
于 2016-01-09T06:34:14.317 回答
0

$ 不是单词的一部分

你可以试试

    STAT=wordh\$

比尝试

     STAT=${STAT/h$/:’}
于 2016-01-09T06:56:54.390 回答