当您双击bash脚本时,Ubuntu会询问用户是否要显示,运行或在终端中运行…
脚本中是否有一种方法可以确定用户是否选择了“Run In Terminal”?
解决方法
严格地说,您无法判断用户在单击脚本后是否选择了“Run In Terminal”,或者启动终端并从那里运行脚本.但是下面的命令可以帮助你,特别是[-t 2].
if [ -t 1 ]; then echo "Standard output is a terminal." echo "This means a terminal is available,and the user did not redirect the script's output." fi
if [ -t 2 ]; then echo "Standard error is a terminal." >&2 echo "If you're going to display things for the user's attention,standard error is normally the way to go." >&2 fi
if tty >/dev/null; then echo "Standard input is a terminal." >$(tty) echo "The tty command returns the name of the terminal device." >$(tty) fi
echo "This message is going to the terminal if there is one." >/dev/tty echo "/dev/tty is a sort of alias for the active terminal." >/dev/tty if [ $? -ne 0 ]; then : # Well,there wasn't one. fi
if [ -n "$DISPLAY" ]; then xmessage "A GUI is available." fi
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。