Redirect grep output to file
You may want to use >>
(append) instead of >
(overwrite) for redirection as:
unzip -p $i | grep -iF "$LOOK_FOR" >> output
Since you're executing this command in a loop and overwriting file output
every time, it might be blank in the end if very last command with grep doesn't find any matching line in unzip output.
You have three problems
- Don't try to parse the output of
ls
. Instead just usefor i in plugins/*source*.jar
The major reason is that your script will completely and utterly break on any files that have spaces in their names. See this link for a litany of reasons why not to parsels
- You need to use
>>
instead of>
as the latter will overwrite the output file on each iteration of the loop. The former will append to it - Use more quotes! You'll want to quote your variables to make sure they aren't subjected to word splitting
Also, you can inline the if
test. So putting it all together we have:
#!/bin/shLOOK_FOR="DefaultProblem"for i in plugins/*source*.jardo # echo "Looking in $i ..." if unzip -p "$i" | grep -i "$LOOK_FOR" >> output #> /dev/null then echo ">>>> Found $LOOK_FOR in $i <<<<" fidone
You can redirect the output of the entire loop:
#!/bin/shLOOK_FOR="DefaultProblem"FILES=`ls plugins/*source*.jar`for i in $FILES ; do # echo "Looking in $i ..." 1>&2 unzip -p $i | grep -i $LOOK_FOR if [ $? == 0 ] ; then echo ">>>> Found $LOOK_FOR in $i <<<<" 1>&2 fidone > output
Note that I've redirected the diagnostic messages to stderr.