[英]Bash - Find a java class and its jar directory by searching whole system
我有以下脚本可以满足我的需要; 也就是说,在整个系统中搜索包含特定Java类文件的jar文件。 我与该脚本的唯一问题是,它会根据传入的类名在jar中找到一个类文件,以使其得到确认。此脚本目前仅将罐中的类包还给我,而不是给我它的jar在,这意味着它没有用。 我正在尝试使用$? 检查搜索命令是否成功,如果是,则将它所在的目录回显到文件中。 尽管它总是返回成功(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
编辑:上面是现在的工作脚本。 通过传入类名,例如./findClass.sh MyClass,它将找到任何.class文件及其jar在系统上的位置。
将整个循环的输出重定向到您的结果文件,而不是将每个命令单独重定向(并使用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
$? 您正在使用tee命令,我敢打赌,它总是成功。 您可能需要Pipe输出并在Bash中捕获退出状态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.