Bash - 通过搜索整个系统找到一个 java class 和它的 jar 目录
Bash - Find a java class and its jar directory by searching whole system
我有以下脚本,它非常接近我的需要;也就是说,在整个系统中搜索包含特定 java class 文件的 jar 文件。我对这个脚本的唯一问题是让它根据我传入的 class 名称在 jar 中找到 class 文件时确认。脚本目前只返回 class 包装在罐子里,而不是它所在的罐子里,这意味着它有点没用。我正在尝试使用 $?检查搜索命令是否成功,如果成功,则将它所在的目录回显到文件中。但是它总是返回 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 .class 1>/dev/null
then
potentialMatches=`jar -tvf $i | grep -Hsi .class`
exactMatch=`echo $potentialMatches | grep -o \/.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 的名称,它将找到任何 .class 文件及其 jar 在系统上的位置,例如./findClass.sh MyClass
$?你正在使用 tee 命令,我敢打赌它总是成功的。你可能想要 Pipe output and capture exit status in Bash
将整个循环的输出重定向到您的结果文件,而不是单独的每个命令(并使用 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 ".class"
then
echo "jar location: $i"
fi
done | tee -a findClassResutls.txt
我有以下脚本,它非常接近我的需要;也就是说,在整个系统中搜索包含特定 java class 文件的 jar 文件。我对这个脚本的唯一问题是让它根据我传入的 class 名称在 jar 中找到 class 文件时确认。脚本目前只返回 class 包装在罐子里,而不是它所在的罐子里,这意味着它有点没用。我正在尝试使用 $?检查搜索命令是否成功,如果成功,则将它所在的目录回显到文件中。但是它总是返回 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 .class 1>/dev/null
then
potentialMatches=`jar -tvf $i | grep -Hsi .class`
exactMatch=`echo $potentialMatches | grep -o \/.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 的名称,它将找到任何 .class 文件及其 jar 在系统上的位置,例如./findClass.sh MyClass
$?你正在使用 tee 命令,我敢打赌它总是成功的。你可能想要 Pipe output and capture exit status in Bash
将整个循环的输出重定向到您的结果文件,而不是单独的每个命令(并使用 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 ".class"
then
echo "jar location: $i"
fi
done | tee -a findClassResutls.txt