I need to open multiple terminal tabs, give them titles, go to a directory, and make each tab run a command.
I am new to Linux and shell scripting, after searching online and checking some solutions, I made this script (EDITED based on answers below):
#!/bin/bash
cd /media/Extra/Project
tab=" --tab-with-profile=Default"
options=(--tab --title=Terminal)
cmds[1]="'rails s'"
titles[1]="Server"
cmds[2]="'rails c'"
titles[2]="Console"
for i in 1 2; do
options+=($tab --title="${titles[i]}" -e "bash -c \"${cmds[i]} ; bash\"" )
done
gnome-terminal "${options[@]}"
exit 0
It opens the tabs, names them, but fail to execute the commands generating this error:
There was an error creating the child process for this terminal
Another shortcoming is that if I halted the running command it closes the tab, which I don't want. I need to be able to stop the command and run it again within the same tab.
What is wrong with the script? Is there another simpler way to do that?
Note: If I removed the (-e "\"bash -c ${cmds[i]} ;bash\"") part from the command, it opens the tabs in the given directory and name them, with no errors.
-Edit-1:
After applying @Tuknutx answer below and editing the script, the error doesn't appear anymore, but it gives me bash: rails c: command not found
and rails s
creates a new rails app instead of starting the rails server, I am using .rmvrc to select a gemset once this folder is accessed.
cmds[1]="'ping 192.168.9.9'"
it gives me the same error but i resolved it by removing single quotation. So if i writecmds[1]="ping 192.168.9.9"
It works.tab=" --tab-with-profile=Default"
??