bash 将 grep 输出重定向到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8876096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect grep output to file
提问by fabricemarcelin
I am not sure as to why that redirection provided in the code does not work. Every time I run the script, the output file is always empty. Does anyone have an idea on that?
我不确定为什么代码中提供的重定向不起作用。每次运行脚本时,输出文件始终为空。有没有人对此有想法?
Thanks.
谢谢。
#!/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
回答by anubhava
You may want to use >>
(append) instead of >
(overwrite) for redirection as:
您可能希望使用>>
(append) 而不是>
(overwrite) 进行重定向,如下所示:
unzip -p $i | grep -i $LOOK_FOR >> output #> /dev/null
Since you're executing this command in a loop and overwriting file output
every time, it might be blank in the endif very last command with grep doesn't find any matching line in unzip output.
由于您在循环中执行此命令并output
每次都覆盖文件,如果最后一个带有 grep 的命令在解压缩输出中找不到任何匹配的行,那么它最终可能是空白的。
回答by SiegeX
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 linkfor 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 appendto it - Use more quotes! You'll want to quote your variables to make sure they aren't subjected to word splitting
- 不要尝试解析
ls
. 而只是使用for i in plugins/*source*.jar
主要原因是您的脚本将在名称中包含空格的任何文件上完全中断。请参阅此链接,了解为什么不解析的一长串原因ls
- 您需要使用
>>
而不是>
因为后者将在循环的每次迭代中覆盖输出文件。前者将附加到它 - 使用更多的报价!您需要引用您的变量以确保它们不会受到分词
Also, you can inline the if
test. So putting it all together we have:
此外,您可以内联if
测试。所以把它们放在一起,我们有:
#!/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
回答by Keith Thompson
You can redirect the output of the entire loop:
您可以重定向整个循环的输出:
#!/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
Note that I've redirected the diagnostic messages to stderr.
请注意,我已将诊断消息重定向到 stderr。
回答by jaypal singh
Instead of a for loop
and an if conditional
you can do everything in one find
command
您可以在一个命令中完成所有操作,而不是 afor loop
和 anif conditional
find
find /path/to/plugins -name "*source*.jar" -exec sh -c 'unzip -l "{}" | grep -q DefaultProblem' \; -print