如何从无限循环停止bash脚本?

我的项目目录中有这样一个名为shrinkwrap.sh的bash脚本:

update_package_json_changed_files() { echo "> changed files" git diff --cached --name-only | grep -x package.json } update_shrinkwrap_and_at_to_git_index() { echo "> Updating npm-shrinkwrap.json and adding to current commit." npm shrinkwrap git add npm-shrinkwrap.json } if get_package_json_changed_files; then update_shrinkwrap_and_at_to_git_index fi 

我在package.json中运行这个脚本如下:

 "scripts": { "shrinkwrap": "bin/shrinkwrap.sh" } 

我安装了npm pre-commit来执行脚本,如下所示:

 "pre-commit": ["shrinkwrap"] 

当我尝试提交时,脚本进入一个无限循环。 它一直在运行,没有什么可以阻止它。 我试过命令+ C,控制+ Z.没有任何工作。 有没有人遇到过这个问题? 为什么会这样呢?

有可能在文件更改时触发它,并至less更改一个文件,这会轮stream触发(在文件更改时),这会使文件更改,等等。

一种常见的方式是产生一个标志文件(touch / tmp / in_progress),并且只有当该文件不存在时才调用update_shrinkwrap_and_at_to_git_index()
如果存在,删除它(但不要做其他事情)

所以:

  • update_shrinkwrap_and_at_to_git_index() touch /tmp/in_progress
  • 之前if get_package_json_changed_files; 添加一个if [ -e /tmp/in_progress ]; then rm /tmp/in_progress; exit 0; fi if [ -e /tmp/in_progress ]; then rm /tmp/in_progress; exit 0; fi

OP Max Doung评论道:

我添加了sudonpm shrinkwarp前,并且它停止了循环。当我把它取出时,循环再次开始。 想知道为什么?

这表明挂钩是由用户本地设置触发的(例如~/.gitconfig一个)

使用不同的帐户(这里是root ,通过sudo )执行命令将避免使用所述全局用户帐户特定设置。