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 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.