4

I have a script that I want to use to open new terminals and type commands in them. A command like

gnome-terminal

opens a new terminal.

Also, if I do

gnome-terminal -e myprogram1

it will execute myprogram1 in the newly opened terminal. But what if, after myprogram1 completes, I want to execute myprogram2? I cannot do something like

gnome-terminal -e myprogram2

because it will open a new terminal.

Is there a way to specify in the script that I want to execute both myprogram1 and myprogram 2 in the same new terminal? Or would I have to create a new script and do something like

gnome-terminal -e scriptToExecuteMyPrograms1And2
CodeBlue
  • 1,117
  • 8
  • 15
  • 22

4 Answers4

8

You can accomplish what you want like this:

$ gnome-terminal -e "bash -c '<cmd1>;<cmd2>;exec $SHELL'"

This will open up <cmd1>, when that's complete, it will open up <cmd2>, finally it will leave you at a command prompt exec $SHELL.

For example:

$ gnome-terminal -e "bash -c 'vim;vim;exec $SHELL'"

Runs vim, if I close the 1st vim, a 2nd vim is started. When the 2nd one closes I'm left at a terminal prompt.

slm
  • 369,824
  • I tried to do exec $SHELL in the beginning,to show the command prompt, but that stopped the rest of the commands from executing. Why so? – CodeBlue Jul 12 '13 at 14:41
  • Because that's what exec does. – tripleee Jul 12 '13 at 14:42
  • I want to see the prompt each time before a command begins execution. – CodeBlue Jul 12 '13 at 14:42
  • 2
    @CodeBlue - exec $SHELL replaces the shell without creating a new process. Seeing the command prompt before each command begins isn't feasible here given there isn't a prompt to show. Bash is run via the -e, it would normally display a prompt, but in this case we're telling Bash to run commands, -c, so cmd#1 runs, when it's done, cmd#2 is run by Bash. When cmd#2 runs, we tell Bash to replace itself (exec ...) with $SHELL, which is /bin/bash. The exec $SHELL is the first time where there is actually a prompt to display. – slm Jul 12 '13 at 15:01
1

Try gnome-terminal -e "myprogram1; myprogram2". You could also try with a && instead of ;.

John
  • 17,011
  • 3
    Keep in mind that myprogram1; myprogram2 is different than myprogram1 && myprogram2. The semicolon ; means that myprogram2 is executed after myprogram1, regardless of its return state (whether execution was successful, or returned an error). && means that myprogram2 is executed only if myprogram1 ran successfully. You can see an example of this when you compile programs from source: ./configure && make && make install means the execution of each successive program is dependent on the successful execution of the previous command - make install won't run if make failed. – MattDMo Jul 12 '13 at 14:26
0

If you want to execute many commands after firing up the terminal then a better approach would be to write a function, export that and call that function. Create a shell script similar to one given below to suit your application:

my_function(){
    command1
    command2
    .
    .
    .
    commandn
}

export -f my_function

$(x-terminal-emulator -e "bash -c 'my_function'")

You can also have more than one function. But, then you will have to write another function which acts like main i.e. which calls all the other functions. So, you set your terminal to execute this main function which will take care of the rest.

perror
  • 3,239
  • 7
  • 33
  • 45
Sharad
  • 101
-2

You can put the programs in ~/.bashrc file

This is valid for bash shell if you are using it. Whenever you open the terminal, eitheir through tty or gnome-terminal it will execute.

Basically, you just add at the end of the file your programs to be executed.

BitsOfNix
  • 5,117
  • 1
    this could be dangerous, or not quite what the OP wants, as those commands would be executed every time you open a bash login shell. Maybe that is what he wants, but I try to keep my .bashrc free of stuff that runs automatically (unless I really want it to run every time, like setting up env. variables) and instead put conditionals in, such as checking to make sure I'm in X, etc. – MattDMo Jul 12 '13 at 14:53