linux - 获取 bash 脚本以打开终端
问题描述
在 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 中启动时都将正确运行。它尝试在内部重新启动脚本
konsole
,xterm
但如果它没有找到它们,它将简单地在后台运行。
推荐阅读
- mysql - 异步功能等待不等待
- kube-apiserver - Kubernetes 集群重启后不运行
- reactjs - 用于 componentDidMount 依赖的 useEffect
- elasticsearch - 查询以比较来自两个不同索引的两列
- json - 使用 Spark Scala 将 Array[Byte] 转换为 JSON 格式
- php - 每次页面刷新时都不会检索来自 mysql 的数据
- python - 如何为我的烧瓶应用程序为在线用户创建 rq 工作人员?(使用redis服务器)
- javascript - 如何在 Cypress 中创建命令
- c - 在 CentOS 上编译 C 到静态库
- flutter - 在 Flutter / Dart 中加载许多资源