Shell脚本 - 获取动态生成的文件的名称

时间:2021-05-08 23:45:29

I'm very new to shell script and therefore I don't now very much about it. I have an application, which creates a java file with a half unknown name, and now I try to write a script, which needs this name.
The known name of the file is /target/plugin-<dyn>.jar, the <dyn> part is unknown and could be nearly anything (btw it is mostly a version number with variable text parts).
Now I want to save plugin-<dyn> (without the .jar) in a variable for later use. I looked very much in the internet, but I can't find a solution.

我是shell脚本的新手,所以我现在对它不太了解。我有一个应用程序,它创建一个名称不明的java文件,现在我尝试编写一个需要此名称的脚本。该文件的已知名称是/target/plugin- .jar, 部分是未知的,几乎可以是任何东西(顺便说一句,它主要是带有可变文本部分的版本号)。现在我想在一个变量中保存plugin- (不带.jar)供以后使用。我非常关注互联网,但我找不到解决方案。

2 个解决方案

#1


1  

If you need get file name without extension .jar. You can refer my bash script below:

如果你需要获取没有扩展名.jar的文件名。您可以在下面参考我的bash脚本:

# for loop all files in target directory that matched plugin-*.jar
for f in target/plugin-*.jar
do 
    # print file name without extension .jar
    echo ${f%.*}
done

UPDATED:

更新:

# for loop all files in target directory that matched plugin-*.jar
for f in target/plugin-*.jar
do 
    # print file name without extension .jar
    filename="${f##*/}" # get plugin-*.jar
    echo ${filename%.*} # print plugin-* without jar
done

#2


0  

try this

尝试这个

filename="/target/plugin-<dyn>.jar"
echo ${filename} | awk -F [/.] '{print $(NF - 1)}'
echo ${filename} | sed 's/.*\/\([^/]*\)\.jar/\1/'

but if <dyn> has a slash, comma or point. it may not work

但如果 有斜杠,逗号或点。它可能无法正常工作

#1


1  

If you need get file name without extension .jar. You can refer my bash script below:

如果你需要获取没有扩展名.jar的文件名。您可以在下面参考我的bash脚本:

# for loop all files in target directory that matched plugin-*.jar
for f in target/plugin-*.jar
do 
    # print file name without extension .jar
    echo ${f%.*}
done

UPDATED:

更新:

# for loop all files in target directory that matched plugin-*.jar
for f in target/plugin-*.jar
do 
    # print file name without extension .jar
    filename="${f##*/}" # get plugin-*.jar
    echo ${filename%.*} # print plugin-* without jar
done

#2


0  

try this

尝试这个

filename="/target/plugin-<dyn>.jar"
echo ${filename} | awk -F [/.] '{print $(NF - 1)}'
echo ${filename} | sed 's/.*\/\([^/]*\)\.jar/\1/'

but if <dyn> has a slash, comma or point. it may not work

但如果 有斜杠,逗号或点。它可能无法正常工作