【问题标题】:Grep across the file system has no output in a shell script跨文件系统的 Grep 在 shell 脚本中没有输出
【发布时间】:2013-09-05 16:19:08
【问题描述】:

我正在尝试在 Git 中创建一个预提交挂钩,它将检查任何调试代码并提示用户修复它。我有一个我正在寻找的正则表达式(忽略它不会排除多行 cmets 中出现的事实!):

grep -IiRn --exclude-dir={node_modules,vendor,public,lib,contrib} --include=\*.{module,inc,install,php,js} -P '^\s*(?!\/\/)\s*(dpm\(|dsm\(|console.log\()' /path/to/code/

当我在控制台中正常运行它时,它工作得很好,但是当我在一个可执行的.sh 脚本中尝试它时,它什么也不做。以下都不适合我:

#!/bin/sh

grep ...

MYVAR =`grep ...` # Note the backticks!
echo $MYVAR

MYVAR =$(grep ...)
echo $MYVAR

MYVAR ="`grep ...`"
echo $MYVAR

我尝试用 Python 和 os.system() 来做这件事,但也没有用。它似乎只是没有标准输出。我可能缺少一些明显的东西,但我处于松散的结局。

任何帮助将不胜感激!谢谢。

编辑:

这是确切的脚本,尽管由于无法实际执行第一部分,它处于最早的可能阶段。我已经隐藏了确切的文件夹名称,因为最好不要在 SO 上共享我公司的代码库;)

#!/bin/bash

echo "Test!"

ONE=`grep -IiRn --exclude-dir={node_modules,vendor,public,lib,contrib} --include=\*.{module,inc,install,php,js} -P '^\s*(?!\/\/)\s*(dpm\(|dsm\(|console.log\()' /company/projects/company/www/sites/all/modules/custom/`

TWO=$(grep -IiRn --exclude-dir={node_modules,vendor,public,lib,contrib} --include=\*.{coffee} -P '^\s*(?!\#)\s*(dpm\(|dsm\(|console.log)' /company/projects/company/www/sites/all/modules/custom/)

echo $ONE
echo "$TWO"

...运行bash -x pre-commit 返回:

ubuntu@ip-12-34-56-78:/company/projects/company/scripts$ bash -x pre-commit
+ echo 'Test!'
Test!
++ grep -IiRn --exclude-dir=node_modules --exclude-dir=vendor --exclude-dir=public --exclude-dir=lib --exclude-dir=contrib '--include=*.module' '--include=*.inc' '--include=*.install' '--include=*.php' '--include=*.js' -P '^\s*(?!\/\/)\s*(dpm\(|dsm\(|console.log\()' /company/projects/company/www/sites/all/modules/custom/
+ ONE='/company/projects/company/www/sites/all/modules/custom/some_module/some_module.report.inc:594:  dsm('\''test'\'');
/company/projects/company/www/sites/all/modules/custom/goals_app/goals_app.module:170:  console.log(e.stack);
/company/projects/company/www/sites/all/modules/custom/company_usage_reports/js/script.js:300:          console.log('\''fetch success'\'');
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_change_workgroup.js:19:    console.log('\''wtf?'\'');
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:33:                  console.log(resp);
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:39:              console.log(ui.placeholder);
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_goal_form.js:4:      console.log($( ".required" ));
/company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder.js:40:                  console.log(resp);
/company/projects/company/www/sites/all/modules/custom/company_goals/js/views/goal-list.js:87:          console.log(data);'
++ grep -IiRn --exclude-dir=node_modules --exclude-dir=vendor --exclude-dir=public --exclude-dir=lib --exclude-dir=contrib '--include=*.{coffee}' -P '^\s*(?!\#)\s*(dpm\(|dsm\(|console.log)' /company/projects/company/www/sites/all/modules/custom/
+ TWO=
+ echo /company/projects/company/www/sites/all/modules/custom/some_module/some_module.report.inc:594: 'dsm('\''test'\'');' /company/projects/company/www/sites/all/modules/custom/goals_app/goals_app.module:170: 'console.log(e.stack);' /company/projects/company/www/sites/all/modules/custom/company_usage_reports/js/script.js:300: 'console.log('\''fetch' 'success'\'');' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_change_workgroup.js:19: 'console.log('\''wtf?'\'');' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:33: 'console.log(resp);' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:39: 'console.log(ui.placeholder);' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_goal_form.js:4: 'console.log($(' '".required"' '));' /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder.js:40: 'console.log(resp);' /company/projects/company/www/sites/all/modules/custom/company_goals/js/views/goal-list.js:87: 'console.log(data);'
/company/projects/company/www/sites/all/modules/custom/some_module/some_module.report.inc:594: dsm('test'); /company/projects/company/www/sites/all/modules/custom/goals_app/goals_app.module:170: console.log(e.stack); /company/projects/company/www/sites/all/modules/custom/company_usage_reports/js/script.js:300: console.log('fetch success'); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_change_workgroup.js:19: console.log('wtf?'); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:33: console.log(resp); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder_table.js:39: console.log(ui.placeholder); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_goal_form.js:4: console.log($( ".required" )); /company/projects/company/www/sites/all/modules/custom/another_module/js/another_module_reorder.js:40: console.log(resp); /company/projects/company/www/sites/all/modules/custom/company_goals/js/views/goal-list.js:87: console.log(data);
+ echo ''

...但是在没有-x 标志的情况下运行它仍然不起作用。

编辑二:

如果有人想知道,我的环境如下......

ubuntu@ip-12-34-56-78:~$ uname -a
Linux ip-12-34-56-78 3.2.0-31-virtual #50-Ubuntu SMP Fri Sep 7 16:36:36 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
ubuntu@ip-12-34-56-78:~$ whereis sh && whereis bash
sh: /bin/sh /bin/sh.distrib /usr/share/man/man1/sh.1.gz
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

【问题讨论】:

  • 变量赋值中等号=前后不能有空格。试试MYVAR=$( .... ),不带空格。另外最好在echo 命令中引用变量,如echo "$MYVAR",否则可能的换行符将转换为空格
  • 感谢您的回复-尽管这些都没有影响。
  • 发布您的实际脚本
  • 很抱歉,这个愚蠢的问题只是为了解决这个问题,但我假设您的脚本位于同一个文件夹中,并且您从运行 @ 时的同一个文件夹中运行它987654334@ 手动(只是为了确保苹果与苹果之间的比较)?
  • 我更新了我的答案以包含这个,但你应该知道 Ubuntu 上的 /bin/sh 指向 dash,一个比 bash 更严格的 shell。

标签: regex bash shell grep


【解决方案1】:

在您发布您正在运行的实际脚本之前,我无法确定,但在您当前的代码中,sn-p 有

#!/bin/sh

根据您的操作系统,这可能是指向 /bin/bash 的链接,例如,也可能是实际的 Bourne shell,即 does not support brace expansion(例如 {a, b, c})。即使/bin/sh 确实指向您机器上的/bin/bash,您也应该只在您的shebang 是#!/bin/sh(即说出您的意思)时才使用可移植结构。如果您想在脚本中使用大括号扩展,请将 shebang 更改为 #!/bin/bash

如果你放

set -x

在脚本的顶部,它将打印有助于调试的详细信息。您也可以通过直接调用 shell 而不是修改脚本来做到这一点,例如

sh -x /path/to/script

bash -x /path/to/script

编辑: 在 Ubuntu 上,/bin/sh 是 dash,即 Debian Almquist shell。与 Bourne shell 一样,dash 具有相当的限制性,并且不支持大括号扩展。有关可移植性问题和破折号的讨论,请参阅 this page

【讨论】:

  • 嗨,谢谢。好吧,实际上没有什么要补充的了。 sn-p 代码,差不多。我尝试改用#!/bin/bash,但没有任何区别。请稍等,我将编辑我的问题...
  • @MattFletcher 实际上,还有更多要补充的:您用省略号替换了大部分脚本。调试你看不到的东西是非常困难的,这可以解释为什么你的问题没有更多的答案。一般来说,在 SO 上,您应该发布完整的代码(复制粘贴您实际运行的脚本),以及输入、预期输出和实际输出。
  • 我这样做只是因为我认为在边缘重复多次的同样很长的代码行会让人更难阅读并分散人们的注意力。我现在已经更新了我的问题,希望能更详细一点:) 干杯
  • +1:正如我在对我的问题的评论中提到的那样,确实需要bash 而不是sh,如果将来有人正在阅读此内容。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2013-12-08
  • 2012-07-26
  • 2019-11-10
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多