2013-09-05 66 views
1

我试图在Git中创建一个预先提交的钩子,它将检查任何调试代码并提示用户修复它。我有我的grepping一个正则表达式(忽略一个事实,即它不会在多行注释排除出现!):跨文件系统的grep在shell脚本中没有输出

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回报:

[email protected]:/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标志STILL不起作用。

编辑二:

如果你想知道,我的ENV如下...

[email protected]:~$ 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 
[email protected]:~$ 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

变量赋值中的等号'='之前或之后不能有空格。尝试'MYVAR = $(....)'没有空格。另外最好在'echo'命令中引用变量,如'echo“$ MYVAR”',否则可能的换行符将被转换为空格 – user000001

+0

感谢您的回应 - 这两件事情都没有改变。 –

+3

发布您的实际脚本 – KevinDTimm

回答

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 

在脚本的顶部,它会打印详细的信息,可以与调试帮助。您也可以通过调用外壳,而不是直接修改你的脚本做到这一点,例如

sh -x /path/to/script 

bash -x /path/to/script 

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

+0

嗨,谢谢。那么,实际上没有什么更多的补充。代码片段*非常多。我尝试用'#!/ bin/bash'来代替,它没有任何区别。给我一秒钟,我会编辑我的问题... –

+0

@MattFletcher其实,还有更多要补充的:你用椭圆代替了你的大部分脚本。调试你看不到的东西是非常困难的,这也许可以解释为什么你的问题没有更多的答案。一般来说,你应该发布完整的代码(复制粘贴你实际运行的脚本*),以及输入,期望输出和实际输出。 – ThisSuitIsBlackNot

+0

我这样做只是因为我认为有相同的非常长的代码重复多次运行边缘会使人们难以阅读和分散人们的注意力。我现在已经更新了我的问题,希望能稍微详细一点:) Cheers –

相关问题