English 中文(简体)
2. 直接放牧产出存档
原标题:Redirect grep output to file
  • 时间:2012-01-16 05:30:00
  •  标签:
  • bash
  • shell
  • sh

我不敢肯定,为什么《法典》规定的这种重新定向不可行。 每当我管理文字时,产出档案总是空洞的。 是否有人对此有想法?

感谢。

#!/bin/sh

LOOK_FOR="DefaultProblem"
FILES=`ls plugins/*source*.jar`

for i in $FILES
  do
    # echo "Looking in $i ..."
    unzip -p $i | grep -i $LOOK_FOR > output #> /dev/null 
    if [ $? == 0 ]
    then
      echo ">>>> Found $LOOK_FOR in $i <<<<"
    fi
  done
最佳回答

您不妨使用<代码>>> (append)而不是> (overwrite) ,作为:

unzip -p $i | grep -iF "$LOOK_FOR" >> output

由于你在档案和超稿档案中重新执行这一指挥系统,<>产出> 每一次,在结尾时,都不得空白。 如果最后用灰.进行指挥,那么在不燃烧的产出中就发现任何配对线。

问题回答

你有三个问题

  1. Don t try to parse the output of ls. Instead just use for 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 parse ls
  2. 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
  3. Use more quotes! You ll want to quote your variables to make sure they aren t subjected to word splitting

另外,您也可在<条码>>>标准上登上。 因此,我们把它放在一起:

#!/bin/sh

LOOK_FOR="DefaultProblem"
for i in plugins/*source*.jar
do
    # echo "Looking in $i ..."
    if unzip -p "$i" | grep -i "$LOOK_FOR" >> output #> /dev/null
    then
      echo ">>>> Found $LOOK_FOR in $i <<<<"
    fi
done

您可以调整整个周期的产出:

#!/bin/sh

LOOK_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
    fi
done > output

请注意,我已将诊断信息转至梯子。

您可以不使用<条码>、<条码>和<条码>。 指挥

find /path/to/plugins -name "*source*.jar" -exec sh -c  unzip -l "{}" | grep -q DefaultProblem  ; -print




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签