Bash 函数在两个分支上运行任意命令

Bash 函数在两个分支上运行任意命令

我想编写一个 bash 函数,在多个分支上执行任意命令。

compare_command () {
    branch2="$1"
    shift
    command="$@"
    echo $branch2
    echo $command
    # assume $@ is command and args
    $(command) 2>&1 | tee baseline.log
    git checkout "$branch2"
    $(command) 2>&1 | tee "$branch2".log
    git checkout -
}

compare_command master ls,例如,失败并显示“命令未找到:1” compare_command master ls -a,失败并显示“命令未找到:ls -a”

答案1

我假设您只是对 bash 中如何使用括号感到困惑

compare_command () {
    branch2="$1"
    shift
    echo "$branch2"
    echo "$@"
    # assume $@ is command and args
    "$@" 2>&1 | tee baseline.log
    git checkout "$branch2"
    "$@" 2>&1 | tee "$branch2".log
    git checkout -
}

相关内容