Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


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

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 – vtest Oct 21 '10 at 20:11
up vote 4 down vote accepted

Try the following change:



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

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
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
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
@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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .