0

在 bash 脚本中,我想复制一个文件,但文件名会随着时间而改变。但是,文件名的开头和结尾将保持不变。

有没有办法让我得到这样的文件:

  cp start~end.jar

哪里〜可以是什么?

如果这有所不同,cp 命令将在 ubuntu 机器上运行 bash 脚本。

4

2 回答 2

4

glob ( start*end) 将为您提供所有匹配的文件。

查看Expansion > Pathname Expansion > Pattern Matchingbash 手册的部分以获得更具体的控制

   *      Matches any string, including the null string.
   ?      Matches any single character.
   [...]  Matches any one of the enclosed characters.  A pair of characters separated by a hyphen denotes a range expression; any character that sorts between those two characters, inclusive, using the current locale's collat-
          ing sequence and character set, is matched.  If the first character following the [ is a !  or a ^ then any character not enclosed is matched.  The sorting order of characters in range expressions  is  determined  by
          the  current locale and the value of the LC_COLLATE shell variable, if set.  A - may be matched by including it as the first or last character in the set.  A ] may be matched by including it as the first character in
          the set.

如果您启用extglob

  ?(pattern-list)
         Matches zero or one occurrence of the given patterns
  *(pattern-list)
         Matches zero or more occurrences of the given patterns
  +(pattern-list)
         Matches one or more occurrences of the given patterns
  @(pattern-list)
         Matches one of the given patterns
  !(pattern-list)
         Matches anything except one of the given patterns
于 2013-03-06T16:16:42.310 回答
2

使用 aglob捕获变量文本:

cp start*end.jar
于 2013-03-06T16:11:55.597 回答