linux - 获取 bash 脚本以打开终端

标签 linux bash terminal

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

最佳答案

你可以做类似于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 并且如果它没有找到它们 只会在后台运行。

关于linux - 获取 bash 脚本以打开终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56686044/

相关文章:

bash - osx 星期一日期本周 bash

ruby - 尝试使用 RVM 安装 Ruby 时运行 '__rvm_make -j8' 时出错

macos - 如何在 osx 上永久更改 .bashrc 文件中的提示?

c - 关于 creat() 系统调用模式参数的快速问题

c - 多个进程访问一个文件

linux - 在 ksh shell 脚本中运行脚本

python - 子进程不引发 CalledProcessError

Linux 帧缓冲区查看器

bash - 从多个文件添加列的简单方法

linux - 创建名称快捷方式以从 Linux 终端运行应用程序