Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
#!/bin/sh

LOOK_FOR="NTLMAuthenticationFilter"

for i in `find ./ -name "*jar"`
do
echo "Looking in $i ..."
grepjar -e $LOOK_FOR $i
done

I wrote the script above, and try to find if there any file name LOOK_FOR exist in those jar,my quest is: grepjar -e $LOOK_FOR $i here how can I check if there are any successful result , and output them ?

by the way, what's mean of $? in shell script ? Thanks !

share|improve this question
    
for i in ... is a known pitfall, never do that. See mywiki.wooledge.org/BashPitfalls –  vtest Oct 21 '10 at 20:11

2 Answers 2

up vote 4 down vote accepted

Try the following change:

#!/bin/bash

LOOK_FOR="NTLMAuthenticationFilter"

find ./ -name "*.jar" -print | while read -r FILE
do
echo "Looking in ${FILE} ..."
grepjar -e "${LOOK_FOR}" "${FILE}" 2>/dev/null >/dev/null
RETCODE=${?}
if [ ${RETCODE} -eq 0 ]; then
  echo "Found ${LOOK_FOR} in ${FILE}"
fi
done

The ? variable stores the return value of the last command. This will normally be 0 for success and any other value for failure.

You'll also notice that I've swiched the for command into a while command. That allows for a larger set of returned results.


Breaking down that find ./ -name "*.jar" -print | while read -r FILE command:

find ./ - starting in the current directory...
-name "*.jar" - find files ending in .jar...
-print - and print their names
| - redirect that output to the next command while read -r FILE - and while there is output from the previous command, assign it to the variable FILE and run the commands between do and done.

In other words, it takes the output of the find command (the list of files ending in jar) and runs the block of code in the loop. For a short list it behaves exactly like your version using the for i in loop. The advantage is that it supports long lists, since there's an upper limit to what you can pass on the command line, as you were doing with the for i in loop.

Edit: Changed to bash script, changed style of variable quoting, included suggestions from comments and some other style changes.

share|improve this answer
    
-print | while read i, can you explain this ? and I found that grepjar are always display error message, can I force it don't output any error message? –  Foolish Oct 21 '10 at 7:20
    
@Foolish: yes ... cmd 2> /dev/null –  akira Oct 21 '10 at 7:26
1  
Anything in /dev is a device. /dev/null is a special device that takes and discards any input. –  Cry Havok Oct 21 '10 at 16:45
1  
If you're checking for $? being zero or non-zero in if then you're doing it wrong. Just put the command directly in the if in the first place. –  Ignacio Vazquez-Abrams Oct 21 '10 at 18:41
1  
@Cry: Change grepjar -e $LOOK_FOR $i to grepjar -e "$LOOK_FOR" "$i", and read i to read -r i, in case the file names contain special character backslashes. Always double-quote variable substitutions and pass -r to read, unless you know why not. –  Gilles Oct 21 '10 at 21:07

You should be able to use $?.

In bash $? stores the return value of a command. You could check this value after the grepjar command and echo $i if it is what you are after.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.