整个文件系统中的grep在shell脚本中没有输出

时间:2013-09-05 16:19:09

标签: regex bash shell grep

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

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(),但这也没有做任何事情。它似乎没有STDOUT。可能有一些显而易见的东西我不知道但是我处于松散状态。

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

编辑:

这是确切的脚本,即使它处于尽可能早的阶段,因为无法实际执行第一位。我隐藏了确切的文件夹名称,因为最好不要在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

1 个答案:

答案 0 :(得分:1)

在发布您正在运行的实际脚本之前,我无法肯定地说,但在您当前的代码段中

#!/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是破折号,Debian Almquist shell。与Bourne shell一样,dash是相当严格的,并且不支持大括号扩展。有关可移植性问题和破折号的讨论,请参阅this page