gpt4 book ai didi

bash - 复制替换文件名字符串中的内容

转载 作者:行者123 更新时间:2023-11-29 09:21:28 25 4
gpt4 key购买 nike

我正在尝试将文件 boot-1.5-SNAPSHOT.jar 复制到 boot-1.5.jar

我已经尝试使用 sed 进行此操作,但出现错误。我知道语法不正确,我希望有人能帮我完成它。

cp boot-* | sed -e 's:-SNAPSHOT::g'
cp: missing destination file operand after ‘boot-1.5-SNAPSHOT.jar’

这样做的想法是脚本不知道版本号,只知道 jar 位于它试图从中复制文件的文件夹中。这就是我使用星号的原因。版本号更改非常频繁,因此使用 cp file file2 将不起作用。

最佳答案

不需要sed,使用Parameter Expansions :

$ file=boot-1.5-SNAPSHOT.jar
$ echo ${file/-SNAPSHOT/}
boot-1.5.jar

$ cp "$file" "${file/-SNAPSHOT/}"

在这种情况下:

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string.

请注意,由于您正在处理文件,因此您应该引用变量(否则带有空格字符的文件会把事情搞砸)。

此外,您还可以创建符号链接(symbolic link)而不是副本:

$ ln -s "$file" "${file/-SNAPSHOT/}"

关于bash - 复制替换文件名字符串中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com