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-
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
但如果