gpt4 book ai didi

linux - 如何编写一个 shell 脚本来打开四个终端并在每个终端中执行一条命令?

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:33 27 4
gpt4 key购买 nike

所以我正在尝试创建一个 shell 脚本来打开四个终端窗口(最好是 konsoles)并在每个窗口中运行一个命令,然后让每个终端保持打开状态,以便我可以在需要时继续在其中执行命令。

我尝试按照此处列出的说明进行操作:

  1. How to create a shell script to launch 3 terminals and execute a set of commands in each?

  1. How can I make a script that opens terminal windows and executescommands in them?

在尝试了这些细节之后,我得到的最好结果如下:

#!/bin/bash

# some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
# I also can't enter text after command executes
#echo "Hello World!"
#exec konsole --noclose -e cat ~/.aliases

# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World"

# didn't do anything
#exit 0

# didn't do anything except make me type exit an extra time where I executed my shell script
#$SHELL

编辑:使用 Roberto 的回答,我得到了四个这样的终端,但我无法输入其他命令,请注意没有像“mycomputername>”这样的提示:

enter image description here

编辑 2:

我找到了一种更好的方法来做我想做的事。下面的脚本将在单独的终端中执行 cmds 数组中列出的命令。所以 echo 'hello1' 将在一个终端中运行,而 echo 'hello2' 将在另一个终端中运行。这将继续执行 cmds 数组中列出的所有命令

!/bin/bash
# Shell script to open terminals
# and execute a separate command in each

# Commands to run (one per terminal)
cmds=('echo 'hello1'', 'echo 'hello2'')

# Loop through commands, open terminal, execute command
for i in "${cmds[@]}"
do
xterm -e "$i && /bin/tcsh" &
done

最佳答案

控制台

多个窗口

#!/usr/bin/env bash
konsole --noclose -e echo Hello terminal 1! &
konsole --noclose -e echo Hello terminal 2! &
konsole --noclose -e echo Hello terminal 3! &
konsole --noclose -e echo Hello terminal 4! &

多个标签页

#!/usr/bin/env bash
konsole --noclose --new-tab -e echo Hello terminal 1! &
konsole --noclose --new-tab -e echo Hello terminal 2! &
konsole --noclose --new-tab -e echo Hello terminal 3! &
konsole --noclose --new-tab -e echo Hello terminal 4! &

关于linux - 如何编写一个 shell 脚本来打开四个终端并在每个终端中执行一条命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42444615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com