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 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
  • 105,154
  • 20
  • 279
  • 497
  • 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