我正在尝试使用 bash 脚本实现确认提示,但由于某种原因,提示不会等待用户输入。我尝试过很多例子,但到目前为止还没有运气。如果有什么区别的话我用的是MacOS。
只是我尝试过的几个例子(所有复制+粘贴来自SO中的其他答案):
#!/bin/bash
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# do dangerous stuff
fi
#!/bin/bash
read -p "Continue (y/n)?" CONT
if [ "$CONT" = "y" ]; then
echo "yaaa";
else
echo "booo";
fi
#!/bin/bash
while true; do
read -rsn1 input
if [ "$input" = "a" ]; then
echo "hello world"
fi
done
#!/bin/bash
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) echo "yes";;
n|N ) echo "no";;
* ) echo "invalid";;
esac
这甚至不会提示任何内容:
#!/bin/bash
read -n 1 -s -r -p "Press any key to continue"
更改为评论中的答案:在 commit-msg 挂钩中,标准输入似乎已关闭,实际上可以通过添加以下命令来检查
ls -l /dev/fd/
这使
... 0 -> /dev/null
如中提到的这个帖子 https://stackoverflow.com/questions/3417896/how-do-i-prompt-the-user-from-within-a-commit-msg-hook
exec 0< /dev/tty
将恢复标准输入到 tty,另一个解决方案注意到标准输出和错误仍然重定向到 tty
exec 0<&1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)