2

I'm looking for a way to run commands from one bash script to 2 different terminals.

I know a way to do it manually by typing "tty" in one terminal and then redirecting the commands from the other terminal with the "tty" output.

But I would like to do it automatically with a script which can save in a variable the output of "tty". Basically I want to launch a script in a terminal which launches a program in another one and print monitoring messages to follow its activity.

I tried :

gnome-terminal -e "bash -c 'term2=$(tty)' ;exec bash'"

or even:

gnome-terminal -e "bash -c 'tty > term2.txt;my_var='cat term2.txt' ;exec bash'"

But none of them is able to save the output of "tty" in my variable. Which I don't understand because it works if I type them manually one by one.

Could you please explain me what is wrong with my method? If you have any suggestions or other methods to propose I'll be glad to learn new things.

  • This can be done relatively easily with screen and tmux. – DopeGhoti Apr 7 '17 at 22:46
  • Thank you for your answer DopeGhoti. I had a look to tmux but is there a way to write the command in a script and not manually do "C-b" to enable tmux command? – user224946 Apr 8 '17 at 0:32
  • Tmux is scriptable. You can create a tmux session that's detached, create two windows/panes and send commands to each. – rovr138 Apr 8 '17 at 4:48
  • And the same applies to screen, for the record (: – DopeGhoti Apr 9 '17 at 4:47
  • Thank you for your answers, I managed to do what I wanted except for one thing: How could we send variables, for example the name of a python script we want to launch in the pane number 1 with "tmux send-keys -t 1 'python $script". If we start the bash script with : script=test.py And we want to send it later via keys without editing each key each time. – user224946 Apr 9 '17 at 23:07
1

Thanks to your hints, I found the answer to my question. Here is an example which I hope could help others in my situation:

I used "tmux" to split a terminal window. The following script enables to launch a python script in the right pane of the window and a bash script in the left one. The "send-key" command enables to send commands to a specific pane. To be able to use your variables in each pane, you can declare them with "export" at the beginning of your script.

export python_script=test.py export bash_script=test_bash.sh tmux new-session -d tmux split-window -h tmux send-keys -t 1 'python $python_script' 'C-m' tmux send-keys -t 0 'bash $bash_script' 'C-m' tmux attach-session

Your Answer

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