4

So I'm trying to create a shell script to do open up four terminal windows (konsoles preferably) and run a command in each and then keep each of those terminals open so I can continue to execute commands in them if desired.

I tried following the instructions listed here:

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

and

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

and after trying those details the best I have is the following:

#!/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

EDIT: Using Roberto's answer I get four terminals like this, but I can't enter additional commands, notice how there is no prompt like "mycomputername> ":

enter image description here

EDIT 2:

I found an even better way to do what I want. The script below will execute the commands listed in the cmds array in a separate terminal. So echo 'hello1' will run in one terminal, and echo 'hello2' will run in another terminal. This will continue for as many commands listed in the cmds array

!/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
Community
  • 1
  • 1
Veridian
  • 3,531
  • 12
  • 46
  • 80

4 Answers4

4

Konsole

multiple windows

#!/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! &

multiple tabs

#!/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! &
MatMis
  • 309
  • 3
  • 16
  • 1
    `multiple windows` works in my kUbuntue v 20.04 . BTW not `multiple tabs` in my simple /bin/bash terminal – SL5net Jan 25 '22 at 11:49
1

You could use a "for" loop, and a "&" to run xterm in background:


#!/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

for i in 1 2 3 4
do
# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World" &
done

# 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 Paz
  • 356
  • 1
  • 8
  • 1
    So that opens up the four terminals. I want each terminal to be executing a different command (e.g. helloworld1, helloworld2,..4), the main problem here is that I can't control any of the terminals. They each open up, display "Hello World", but there is no command line where I can enter additional text in these new windows. I posted a picture in the question so you can see what I'm talking about. – Veridian Feb 24 '17 at 18:37
  • Then your command must be "/bin/bash" instead of "echo Hello My World" – Roberto Paz Feb 24 '17 at 19:02
  • 1
    Or maybe "echo Hello My World && /bin/bash". This will execute first commando (echo), and will execute shell if first command ends correctly. – Roberto Paz Feb 24 '17 at 19:06
  • okay I did the command in your second comment, but when I type exit the terminals don't close – Veridian Feb 24 '17 at 21:29
  • Avoid "-hold" on "xterm" command: "xterm -e "echo Hello World && /bin/bash" &" – Roberto Paz Feb 26 '17 at 03:40
1

I found this to be quite easily:

#!usr/bin/env bash
echo "Enter the value of n:"
read n
for ((i = 0; i < n; i++ ))
do 
   xterm -hold -e <enter command> &
   # In my case, I used : 
   # xterm -hold -e sar -P $i 2 5 &
done

And that's pretty much it! Have a good day :)

Note : For those who are newbies, we save this with a file name '.sh'. Also, please note that this will execute n different commands on n different terminals. If you want, you can execute the same command on every terminal, just remove $i from the in do .... done part ;)

0

On a Linux Mint mate distribution, this will run <commands> in 3 separated terminal windows:

$ cat START.sh

mate-terminal --execute bash -c "<command1>" 
mate-terminal --execute bash -c "<command2>" 
mate-terminal --execute bash -c "<command3>" 

Killing START.sh won't terminate children <commands>.

NVRM
  • 11,480
  • 1
  • 88
  • 87