加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何判断用户是否选择“Run In Terminal”

发布时间:2020-12-14 00:55:03 所属栏目:Linux 来源:网络整理
导读:当您双击bash脚本时,Ubuntu会询问用户是否要显示,运行或在终端中运行… 脚本中是否有一种方法可以确定用户是否选择了“Run In Terminal”? 解决方法 严格地说,您无法判断用户在单击脚本后是否选择了“Run In Terminal”,或者启动终端并从那里运行脚本.但是
当您双击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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读