bash - Mainscript 在新的终端窗口中执行下标
问题描述
“Debian 9 64x - LXDE”
我尝试在 bash 中创建一个安装脚本。假设我想安装 samba。我从主脚本调用 samba 的安装脚本\folder\samba.sh
。脚本 samba.sh 应该在一个新的终端窗口中执行,所以我可以观察安装错误。
该脚本应该像下面的描述一样工作:
- 该脚本
/mainscript.sh
仅提供用户信息、交互,并执行多个下标 (/folder/subscripts.sh
)。 - 该脚本
/mainscript.sh
需要创建一个新的终端窗口,传递路径和名称,subscript.sh
并在新的终端窗口中执行它们。 - 脚本
/mainscript.sh
一次只能执行一个下标(/folder/subscript.sh
)!如果 asubscript.sh
正在运行,则主脚本必须等到新的终端窗口关闭。 - 以
subscript.sh
root权限执行一些代码。
问题:
如何创建一个新的终端窗口,传递下标,并在新的终端窗口中执行它?
如何确保脚本 (
mainscript.sh
) 当时只运行一个下标 (subscript.sh
)?
例子:
主脚本.sh
#!/bin/sh
# This is the content of the mainscript.sh
# subscript1 and subscript2 must not be executed at the same time!
# the mainscript needs to wait when a subscript gets executed!
echo "hello, this script runs in terminal window (((A)))"
xterm /opt/subscript1.sh
echo "samba - Installed"
xterm /opt/subscript2.sh
echo "samba - removed"
下标1.sh
#!bin/sh
# This is the content of the subscript1
echo "This script runs in a new terminal window (((B)))"
apt-get install samba
# instructions done .... close the terminal window (((B))) now
下标2.sh
#!bin/sh
# This is the content of the subscript2
echo "This script runs in a new terminal window (((C)))"
apt-get remove samba
# instructions done .... close the terminal window (((C))) now
解决方案
在澄清您实际上希望在 LXDE 中出现一个新的终端窗口之后,这是一个可能的解决方案。
Debian LXDE 很可能安装了 xterm 或 lxterminal。下面的示例使用 lxterminal。对于 xterm 使用“xterm -e 命令”
首先在自己的窗口中执行 manscript.sh:
$ lxterminal --command=/mainscript.sh
#!/usr/bin/sh
<section that provides user information>
# Call subscripts that will run in sequence
lxterminal --command=/folder/subscripts.sh
当 subscripts.sh 完成时,新的终端窗口将关闭并将控制权返回给 mainscript.sh 通过依次调用它们,您一次只能运行一个下标。