找到grep在命令行中工作但在脚本中失败; shell自动转换?

时间:2013-02-25 20:53:33

标签: shell command-line grep find

更新的:
在再次尝试之后,事实证明该脚本完全按照预期工作。我一定是太累了,做了一些愚蠢的事情,这让我觉得剧本是错的,而实际上是我自己。 :-(
很多thx为你的帮助chepner,shellter,威廉,格伦和乔纳森,我想我的混乱实际上是通过我在当前目录上执行查找来触发的。“。” 感谢提示!

此线程已准备好关闭。

  

请考虑以下事项:

find . | grep -E ".*\.log$"
     

如果在命令行运行,则按预期工作,即   打印出以.log

结尾的所有目录和文件      

但是在脚本中,例如:

#!/bin/sh
find . | grep -E ".*\.log$"
     

失败了。当打开set -x进行检查时,我注意到了   脚本执行正则表达式。*。log $ 是   (auto- ??)引用单引号,如:

\+ find .
\+ grep -E '.*\.log$'
     

即使我的代码包含双引号。 我该如何预防?   此?

     

限制:

     
      
  • 挑战是同时使用findgrep,不允许其他cmd
  •   
  • 同样,挑战是使用两者,而不仅仅是find,因为有人可能会建议
  •   
  • 更多信息,用于grep的正则表达式要复杂得多,如此处所示(几个OR分开   RegExps),我在这里简化了它,因为烦恼发生在这里   一个已经
  •   
     

我一直在研究这个问题超过4个小时没有结果。我搜索过了   谷歌和StackOverflow,但我发现没有类似的东西。我担心的   解决方案非常简单,我找到它太愚蠢了? : - (

     

提前获得任何帮助!

1 个答案:

答案 0 :(得分:0)

您使用的操作系统和可执行文件是什么?

你的小脚本:

#!/bin/sh
find . | grep -E ".*\.log$"

#!/bin/bash
find . | grep -E ".*\.log$"

使用

在Debian Wheezy上为我完美地工作(单个或双反斜杠)
yba@tavas:~$ bash --version
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
yba@tavas:~$ grep --version
grep (GNU grep) 2.12
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

你的脚本在什么意义上失败了?

以下是示例输出:

yba@tavas:~$ ./st.sh 
./Downloads/serial_update_rev11.log
./.virt-manager/virt-manager.log
./Android/adt-bundle-linux/eclipse/configuration/org.eclipse.update/install.log
./Android/workspace/.metadata/.plugins/org.eclipse.cdt.core/.log
./Android/workspace/.metadata/.log
./ti-dvsdk_dm365-evm_4_02_00_06/xdctools_3_16_03_36/eclipse/plugins   /org.eclipse.rtsc.xdctools.product_3.16.3.36/META-INF/Make.log
./ti-dvsdk_dm365-evm_4_02_00_06/codecs-dm365_4_02_00_00/packages/ittiam/codecs/g711_dec    /hs_err_pid17410.log
./.local/share/gvfs-metadata/root-dfdae0ca.log
./.local/share/gvfs-metadata/label-AR7266WnX\x20V2_1.2-a6becf19.log
./.local/share/gvfs-metadata/home-2def37e1.log
./.local/share/gvfs-metadata/uuid-5c7747c5-81e5-4464-bae0-2c61afc73da4-7a539d2d.log
 ./.local/share/tracker/tracker-store.log
./.local/share/tracker/tracker-miner-fs.log
./.local/share/tracker/tracker-extract.log
yba@tavas:~$