pg2455
pg2455

Reputation: 5148

Finding a Jar that contains Main: Shell Scripting

I want to write a shell script that can help me find the jar that contains Main. I had been thinking of:

find . -name "*.jar" | jar -tvf | grep Main

This seems to be incorrect because jar -tvf takes in one input at a time and pipes create full output at once. Is there a possibility to make pipes work serially?

Upvotes: 1

Views: 28

Answers (1)

anubhava
anubhava

Reputation: 785246

If you're using bash you can use this script using process substitution:

while IFS= read -d '' -r file; do
   if jar -tf "$file" | grep -q 'Main'; then
      echo "$file has Main"
      break
   fi
done < <(find . -name "*.jar" -print0)

For non-bash use:

find . -name "*.jar" -print0 |
while IFS= read -d '' -r file; do
   if jar -tf "$file" | grep -q 'Main'; then
      echo "$file has Main"
      break
   fi
done

Upvotes: 3

Related Questions