Windows上的sh shell脚本导致错误:/ dev / tty:没有这样的设备或地址

问题描述 投票:0回答:1

在Windows中的git commit-msg我想运行以下脚本:

MSG="$1"
if ! grep -s -qE "#[0-9]" "$MSG";then
    exec < /dev/tty
    cat "$MSG"
    echo "Your commit message does not contain a reference to a work item. Continue? [y/n]"
    read -s -n 1 sure
    if [[ $sure == "y" ]]; then
        exit 0
    else
        echo "Aborting commit..."
        exit 1
    fi
fi

我使用Git扩展时运行正常。

但是当我想直接从Visual Studio(团队资源管理器或Git更改)提交时,会出现以下消息的错误:

Your git-hook, '.git/hooks/commit-msg' (line 23) is not supported, likely because it expects interactive console support./r/nSee your administrator for additional assistance.

我的问题:是否有可能检查,是否可以执行exec </ dev / tty?否则我只会打印相应的消息。

提前致谢。

git sh githooks
1个回答
2
投票

您可以使用if [ -t 1 ]if [ -t 2 ]来测试您是在管道中还是在终端中。

if [ -t 1 ]; then    # or if [ -t 2 ]
   # do interactive stuff
else
   # non-interactive variant
fi

参考:How to detect if my shell script is running through a pipe?

编辑:OP报告说[ -t 1 ]在我原来的答案中没有用,但[ -t 2 ]确实如此。第一次测试stdout进入tty,而第二次测试stderr进入tty。在这种情况下,我猜测Git Extensions和Visual Studio都捕获脚本的标准输出,但Git Extensions将stderr发送到tty,而Visual Studio也捕获它。

© www.soinside.com 2019 - 2024. All rights reserved.