0

I have to open multiple xterm windows and save them later to run different commands in each of them while keeping them alive.
An example (not correct at all) would be:

term1= xterm -hold -title "term1"
term2= xterm -hold -title "term2"
term3= xterm -hold -title "term3"

Compute same commands for all of them:

for i in term1 term2 term3
do 
  $i -e "cd somewhere; source something"
done

then run different commands for some of them:

$term1 -e "cd somewhere else; ./..."
$term2 -e "do other thing"

I have very little experience in shell scripts, as you may notice, if someone could help me I would be really happy.

EDIT: add example in response to @dessert

Disclaimer: I'm currently working in the ROS ambient.

The script should open four terminal (either xterm, terminator, default terminal) windows; three of them must source a bash script in the same folder and then roscd to another one, of this two of them must execute various commands (one has to launch a launch_file while tho other has to run a script). The fourth one must cd to a different folder where he has to source a .bash and then launch another launch_file. It would be even better if the fourth one can wait x seconds before launching.

ThatGuy
  • 21
  • 1
  • 1
  • 5
  • 1
    At least related: https://askubuntu.com/questions/954051. My answer there explains most of it, however I'm adding an answer for your use case here as well. – dessert Oct 30 '17 at 16:06
  • 2
    Have a look at `tmux` -- you can split a single terminal into several "panes", and send commands simultaneously to all of them. – glenn jackman Oct 30 '17 at 16:11
  • Sorry, I didn't understand the question correctly at first. You should have a look at `screen` ([website](https://www.gnu.org/software/screen/))! – dessert Oct 30 '17 at 16:25
  • Could you provide an example what exactly you need this for? It looks a bit like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me… – dessert Oct 30 '17 at 21:05

1 Answers1

1

Use functions ! The advantage of this method is that you can add other optional positional parameters to function call when you run it.

term1(){
    xterm -hold -title "term1" "$@"
}

Place this into your ~/.bashrc file and run source ~/.bashrc. Same idea for other commands.

In order to keep terminal "alive", you need to spawn shell at the end, i.e. do

term1 -e "cd place1; command 2;bash"

Terminal itself is just a window, and it doesn't have shell alone for you to interact with, so unless you spawn one - you won't get interactive shell.

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • While this is a good solution it doesn't allow to send further commands to an already open terminal window like OP asked for in the *then run different commands* section of the question. – dessert Oct 30 '17 at 21:02
  • @dessert I addressed that in the edit. – Sergiy Kolodyazhnyy Oct 30 '17 at 21:25
  • Thanks for the answer @dessert, just one thing. I initially wanted to do everything in one shell script, is it possible? What i mean is to run the "different commands" in the shell right after the "same ones". – ThatGuy Nov 01 '17 at 17:32