gpt4 book ai didi

java - Bash - 通过搜索整个系统来查找 java 类及其 jar 目录

转载 作者:行者123 更新时间:2023-11-30 08:13:06 25 4
gpt4 key购买 nike

我有以下脚本,它接近我的需要;也就是说,在整个系统中搜索包含特定 java 类文件的 jar 文件。我对这个脚本的唯一问题是让它根据我传入的类名在 jar 中找到类文件时进行确认。目前的脚本只返回 jar 内的类包,而不是它的 jar是在,这意味着它毫无用处。我正在尝试使用 $?检查搜索命令是否成功,如果成功,则将其所在的目录回显到文件中。但它总是返回 success(0),因此它找到的每个 jar 位置都会附加到文件中。我现在不再说话了,有人可以运行这个脚本并看看它正在做什么以及我正在尝试做什么吗?

if [ $# -ne 1 ]
then
echo "Need to provide class name as argument"
exit 1
fi

> findClassResults.txt

for i in $(locate "*.jar");
do
if [ -d $i ]
then
continue
fi

if jar -tvf $i | grep -Hsi $1.class 1>/dev/null
then
potentialMatches=`jar -tvf $i | grep -Hsi $1.class`
exactMatch=`echo $potentialMatches | grep -o \/$1.class`
if [ ! -z $exactMatch ]
then
echo "matches found: " $potentialMatches >> findClassResults.txt
echo ".....in jar @: " $i >> findClassResults.txt
echo -e "\n" >> findClassResults.txt
fi
fi
done

编辑:以上是现在的工作脚本。它将通过传入类的名称(例如,)来找到任何 .class 文件及其 jar 在系统上的位置。 ./findClass.sh MyClass

最佳答案

将循环的输出作为一个整体重定向到结果文件,而不是单独的每个命令(并使用 while 循环来迭代结果,而不是 for循环):

< <(locate "*.jar") while read -r i
do
if [ -d "$i" ] #mvn repos have dirs named as .jar, so skip...
then
continue
fi
if jar -tvf "$i" | grep -q -Hsi "$1.class"
then
echo "jar location: $i"
fi
done | tee -a findClassResutls.txt

关于java - Bash - 通过搜索整个系统来查找 java 类及其 jar 目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078892/

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