exit
,那么对于某些函数还会有其他意外的执行.是什么造成的?在将
git alias作为回答
another user’s question on StackOverflow的一部分时,首先注意到该行为.该别名由此脚本组成(它运行该函数两次而不是一次):
#!/usr/bin/env bash github(){ echo github; }; twitter(){ echo twitter; }; facebook(){ echo facebook; }; if [[ $(type -t "$1") == "function" ]]; then "$1"; else echo "There is no defined function for $1"; fi;
#!/usr/bin/env bash github(){ echo github; }; twitter(){ echo twitter; }; facebook(){ echo facebook; }; if [[ $(type -t "$1") == "function" ]]; then "$1"; exit 0; else echo "There is no defined function for $1"; exit 1; fi;
这正是我通过git别名运行这些脚本时所发生的事情(添加set
命令仅用于调试目的):
$git config --global alias.encrypt-for '!set -evu -o pipefail;github(){ echo github;};twitter(){ echo twitter;};facebook(){ echo facebook;};if [[ $(type -t "$1") == "function" ]];then "$1"; exit 0; else echo "There is no defined function for $1"; exit 1; fi;' $git encrypt-for "github" type -t "$1" github $git config --global alias.encrypt-for '!set -evu -o pipefail;github(){ echo github;};twitter(){ echo twitter;};facebook(){ echo facebook;};if [[ $(type -t "$1") == "function" ]];then "$1"; else echo "There is no defined function for $1"; fi;' $git encrypt-for "github" type -t "$1" github github
set -x的输出:
$git encrypt-for "github" ++ type -t github + [[ function == \f\u\n\c\t\i\o\n ]] + github + echo github github + github + echo github github
使用echo“我在github中回显”替换echo github的输出作为排除echo命令作为第二个函数执行源的方法:
$git encrypt-for "github" ++ type -t github + [[ function == \f\u\n\c\t\i\o\n ]] + github + echo 'I am echo in github' I am echo in github + github + echo 'I am echo in github' I am echo in github
以下是别名/脚本的最简单版本,它提供了双重执行的不良行为:
g(){ echo "once"; }; $1;
这是执行简化别名/脚本(具有执行两次的错误行为)的结果输出:
$git config --global alias.encrypt-for '!g(){ echo "once";};$1;' $git encrypt-for g once once