bash 上的安装程序:如何显示 GUI 消息?

Installer on bash: how to show GUI messages?

我想创建简单的 .sh 脚本,它将二进制包包含在我的程序中并将其复制到目标文件夹。在安装过程中,我想显示 gui 消息以告知用户一切正常。似乎这个问题的热情是我需要 How to show a GUI message box from a bash script in linux? 但是如何用我的单个 .sh 脚本提供它呢? (用户应该 运行 从任何地方安装,无需任何额外操作)。 linux 的最常见发行版是否有通用的东西?也许"xmessage",但看起来很差。还有别的吗?

xmessage 或 zenity 或 gxmessage 之类的任何东西都意味着您无法保证可用的外部依赖项(除非您可以;您在问题中没有这样说)。要回答您的问题之一,NO,Linux 没有任何通用性。当然不是任何依赖于 X 的东西,因为很多 Linux 安装都是无头的。

对于"something else",作为一般原则,独立是一个好主意。这意味着使用甚至不依赖于 X Window 系统的东西。 Shell 基于对话框随时可用,无论您在 FreeBSD or Linux

要真正独立且可移植(甚至在 Linux 的不同发行版或不同的服务器配置之间),我建议将您自己的对话管理器编写为 [=25] 中的一个函数=] 脚本。类似这样的事情:

#!/usr/bin/env bash

# A supporting function to see test a value against the contents of an array
is_in() {
  value=; shift
  for i in "$@"; do [[ $i == $value ]] && return 0; done
  return 1
}

# Simple dialog implementation, no VT100 required,
dialog() {
  # $options is an array of options
  local i line max=0
  # determine dialog width
  for line in "${options[@]}"; do [[ ${#line} -gt $max ]] && max=${#line}; done
  # draw a line
  eval printf '%.s-' {1..$((max+8))}; echo
  # print each option
  for i in ${!options[@]}; do
    printf "| %2d: %-${max}s |\n" "$i" "${options[i]}"
  done
  eval printf '%.s-' {1..$((max+8))}; echo
  response=""
  # only accept valid responses
  while ! is_in "$response" "${!options[@]}"; do
    read -p "  Choose: " response
  done
  return "$response"
}

# Create our list, run the dialog, capture the result,
options=([1]="Hello world" [2]="This is a test")
dialog
result=$?

# And display the result.
echo "RESPONSE: $result / ${options[$result]}"