9

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.

  • If you want to make your script run on your ubuntu boot: - you can run you script as an upstart service(help.ubuntu.com/community/UbuntuBootupHowto) or: using "startup applications" in your desktop environment – Aboelnour Jul 23 '14 at 16:55
  • if i write cmds[1]="'ping 192.168.9.9'" it gives me the same error but i resolved it by removing single quotation. So if i write cmds[1]="ping 192.168.9.9" It works. – d a i s y Feb 24 '16 at 11:23
  • @Brary but i don't understand why it opens three tabs-one default and another two that set in script. Because we define tab=" --tab-with-profile=Default" ?? – d a i s y Feb 24 '16 at 11:26
3

I would recommend using tmux with tmuxinator, it will does the job for you, and you can rely on terminator layouts too!

For terminator layout checkout mhnagaoka's answer here askubuntu too:

  1. After setting up your layout, right-click on any terminal background and choose PreferencesLayouts tab and click on Add button.

  2. Give it a name and hit Close.

  3. This should create the mentioned ~/.config/terminator/config file.

  4. Now you can start terminator using the saved layout using: terminator -l yourLayout (replace yourLayout with whatever you chose on step 2).

  5. (optional) Edit the ~/.config/terminator/config file so that where it says [layouts] and nested below it [[yourLayout]], rename yourLayout to default and remove/rename the previous default layout. Now, when Terminator starts without any parameters, it will load your custom [[default]] layout!

2

To summarize a couple of the other answers and their comments, here is what I ended up with:

#!/bin/bash

cd /path/of/my/stuff

tab=" --tab"
options=()

cmds[1]="echo Banana"

cmds[2]="echo Cat"


for i in 1 2; do
options+=($tab -e "bash -c '${cmds[i]} ; bash'" )
done

gnome-terminal "${options[@]}"

exit 0

Note that I took away the 'master' tab, and the profiles, and the titles (which appear to be deprecated in gnome-terminal).

1

Try this:

options+=($tab --title="${titles[i]}"  -e "bash -c \"${cmds[i]} ; bash\"" ) 

otherwise the whole expression after -e will be interpreted as the command.

To include aliases from .bashrc use -ic instead of c

  • The error doesn't appear anymore, thanks. But it gives me bash: rails c: command not found and 'rails s' creates a new rails app instead of starting the rails server. This changes the scope of the question, I will edit it with ur modification for the script – Brary Jul 20 '14 at 20:42
  • Did it work if you try rails c on a terminal without anything else? Perhaps it should be rails -c ? – TuKsn Jul 20 '14 at 20:55
  • Tried that didn't work. I even tried rvm list gemsets and it says No command 'rvm' found .. run the same command rvm list gemsets in the first tab that opens without running a command and it worked! does it run the command before the environment loads and the .rvmrc file is loaded? – Brary Jul 20 '14 at 21:09
1

The argument you're giving the -e option is "bash -c command; bash" including the quotes. It interprets that whole string as the name of a command! Try this instead: -e "bash -c 'command ; bash'". This way what gets run on your terminal window is command, and after that runs, you're given a daughter shell, which I assume is what you want. Incidentally, you can also say 'command & bash'; this will run the command on the background and give you the daughter shell right away.

  • I changed this options+=($tab --title="${titles[i]}" -e "\"bash -c ${cmds[i]} ;bash\"" ) into options+=($tab --title="${titles[i]}" -e "\"bash -c '${cmds[i]} ;bash'\"" ) and it gave me the same error .. am I doing it right? – Brary Jul 20 '14 at 20:21
  • Not quite correct: remove \" before the first bash and \" after the ' after the second bash. – Silvio Levy Jul 20 '14 at 21:07
  • Thanks, I edited the question and changed the scope of it to include rails and rvm. – Brary Jul 20 '14 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.