首页 > 解决方案 > 获取 bash 脚本以打开终端

问题描述

在 Windows 中,当我双击批处理脚本时,它会自动打开一个终端窗口并显示正在发生的事情。如果我在 Linux 中双击 bash 脚本,终端窗口不会打开以显示正在发生的事情;它在后台运行。我已经看到可以使用一个脚本在新的终端窗口中使用 启动另一个脚本x-terminal-emulator -e "./script.sh",但是我可以将任何 bash 命令放入同一个(一个)script.sh中,以便它打开一个终端并告诉我正在发生的事情(或者如果我需要回答 y/n 个问题)?

标签: linuxbashterminal

解决方案


你可以做一些类似于Slax 开发者在他们的bootinst.sh

#!/usr/bin/env sh
#
#     If you see this file in a text editor instead of getting it executed,
#     then it is missing executable permissions (chmod). You can try to set
#     exec permissions for this file by using:  chmod a+x bootinst.sh
#
#     Scrolling down will reveal the actual code of this script.
#



# if we're running this from X, re-run the script in konsole or xterm
if [ "$DISPLAY" != "" ]; then
   if [ "$1" != "--rex" -a "$2" != "--rex" ]; then
      konsole --nofork -e /bin/sh $0 --rex 2>/dev/null || xterm -e /bin/sh $0 --rex 2>/dev/null || /bin/sh $0 --rex 2>/dev/null
      exit
   fi
fi

# put contents of your script here
echo hi

# do not close the terminal immediately, let user look at the results
echo "Press Enter..."
read junk

此脚本在图形环境和 tty 中启动时都将正确运行。它尝试在内部重新启动脚本 konsolexterm但如果它没有找到它们,它将简单地在后台运行。


推荐阅读