It seems the behavior of gnome-terminal has changed between the version shipped with Ubuntu 14 (v3.6?) and Ubuntu 18 (v3.28).
I have a script that opens a new gnome-terminal with a bunch of tabs setup to different directories for my development, and currently the first tab runs a script. The command to open the gnome-terminal with tabs looks something like this:
gnome-terminal \
--tab --command="myscript.sh" \
--tab --working-directory="<some dir 1>" \
--tab --working-directory="<some dir 2>" \
...
This works perfectly as desired in the gnome-terminal version that shipped with Ubuntu 14 (v3.6?).
But in the gnome-terminal version that ships with Ubuntu 18 (v3.28) several things have changed:
Unless I add the --window option, the tabs open in the current gnome-terminal, not a new one. Unfortunately adding the --window option opens an initial blank tab. Is it possible to open a new window with only the tabs that I specify?
I now get the following notice (though it functions as before):
# Option “--command” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
Changing my script per this guidance changes the behavior such that the command is issued to all tabs, whereas before I could apply a unique command to each tab. Does this mean the ability to run a separate command per tab has been deprecated, or am I missing something?
I appreciate suggestions on how to change my script to support the old behavior in the newer gnome-terminal.
1) Use --window for your first tab
gnome-terminal \
--window -t 'Tab 1' \
--tab -t 'Tab2' --working-directory="<some dir 1>" \
--tab -t 'Tab3' --working-directory="<some dir 2>" \
...
Unfortunately this will only allow one command to be passed in using the new design, and the window/tabs close at completion (I'm not sure if that was the behavior before)
2) If you don't care about the tab closing when the command is complete, you could do this:
$ gnome-terminal --window -- ./mytabs.sh
mytabs.sh
#!/bin/bash
gnome-terminal --tab -t 'Tab 1' -- ./myscript.sh
gnome-terminal --tab -t 'Tab 2' --working-directory="<some dir 1>"
gnome-terminal --tab -t 'Tab 3' --working-directory="<some dir 2>"
This will open each tab from the script in the window that was created in the code above it. It's a pain in that you either have to type out the first command or create a second script.
Related
I have a bash script which opens several tabs inside one window and I want to choose one tabe to be the default focus
for example the below script opens 3 tabs on one window:
gnome-terminal --tab --geometry="100x20" --title="TAB1" -- bash -ic "command1"
gnome-terminal --tab --geometry="100x20" --title="TAB2" -- bash -ic "command2"
gnome-terminal --tab --geometry="100x20" --title="TAB3" -- bash -ic "command3"
So, let's say I want tab2 to be the focus after running the script and openning the window. Is there a way to specify that from the script?
I highly recommend installing terminator, it's a program which offers flexible management of multiple running gnome terminals. It allows you to choose a tab as the default focus while still viewing any other amount of terminals on the same view. It can be installed via:
$ sudo apt install terminator
How do you programmatically opening a terminal application, like Gnome Terminal, and running cd /some/path; source ./setup.bash? I'm trying to write a script that will automatically launch some common terminals and IDEs for work.
I tried:
gnome-terminal --tab --working-directory="/some/path" -e 'source ./setup.bash'
but that launches a gnome-terminal window, but the window shows the error:
Failed to execute child process "source" (No such file or directory)
Presumably, that's because it's not executing the command in bash, so I instead tried:
gnome-terminal --tab --working-directory="/some/path" -e 'bash -c "source ./setup.bash"'
However, that seems to do nothing at all. It launches no window nor produces any stdout or stderr output.
The closest I could get was:
gnome-terminal --tab --working-directory="/some/path" -e 'bash -c "source ./setup.bash; bash -i"'
That launches gnome-terminal and seems to source setup.bash correctly, but some of the terminal formatting set by setup.bash isn't shown, presumably because I'm launching a new bash shell.
Is there a better way?
When you use the -e option the gnome-terminal will run that command without starting a new shell (you can even run something like: gnome-terminal -e gedit), so if you want to run a command into the bash shell into a new terminal/tab you have to do something like this:
gnome-terminal -x bash -c "command"
But note that when "command" ends the terminal/tab will end too.
You can specify the bash startup file to set variables. You might want that file to have source $HOME/.bashrc in it:
$ gnome-terminal --working-directory="/some/path" -e 'bash --rcfile ./setup.bash -c gdb'
You can put a command in after that,as I have -c gdb.
I'm trying to write a script that opens 3 terminal windows and runs a couple of statements in those windows. But it's not working. I've tried using the && operator as well as " " but I can't get it to work. I've also tried it with the statements on the same line as well as below each other. The error I'm receiving is that the cd child process failed to execute stating that there is no such Directory. But the directory ~/Projects/catkin_ws is correct.
#!/bin/bash
# ROS opstarten
gnome-terminal -e cd ~/Projects/catkin_ws source devel/setup.bash roscore
# gazebo opstarten
gnome-terminal -e cd ~/Projects/catkin_ws
source devel/setup.bash
roslaunch cvg_sim_gazebo Qr_Chessboard.launch
# programma opstarten
gnome-terminal -e cd ~/Projects/catkin_ws
source devel/setup.bash
/usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py
If you really want to run them on separate terminals programmatically you can use a terminal multiplexer such as GNU screen for that.
First you have to start a session:
$ screen -S demo
Then open all the terminals you need inside it with Ctrl-a c and configure their environments as needed, and then you can send commands to any screen page (tab) from your script using the "-X stuff" option (to stuff characters into a virtual screen terminal):
$ screen -S demo -p <page_number> -X stuff 'ls -l
'
Note that you also have to send the newline character to really enter the command.
I try add main enviornment path to bash and I success run roscore in another terminal.
#!/bin/bash
# ROS opstarten
PATH=/opt/ros/kinetic/bin
gnome-terminal --tab -e /opt/ros/kinetic/bin/roscore
You need to quote the statements and use a statement separator between them.
gnome-terminal -e 'cd ~/Projects/catkin_ws; source devel/setup.bash; /usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py'
or alternatively with newline as statement separator
gnome-terminal -e 'cd ~/Projects/catkin_ws
source devel/setup.bash
/usr/bin/python /home/user/Projects/catkin_ws/src/drone7_project/src/drone_program.py'
However, running these commands in a separate terminal seems rather misdirected. Why don't you run them as regular background jobs in your current terminal with output to a file?
I'm new to scripting.
I’m using the below code in my project, but the terminal is getting closed instantly. I want the hold the terminal and keep running
#!/bin/bash/
gnome-terminal --tab --working-directory="/home/sandhya/OpenBTS/public/openbts/trunk/apps/" -e "file = grep OpenBTS /home/sandhya/OpenBTS/public/openbts/trunk/apps \
if [ -f $file ] \
then \
echo " file exits" \
sudo "/home/sandhya/OpenBTS/public/openbts/trunk/apps/OpenBTS" \
fi"
I want the terminal to be stay and keep running the application.
Please correct me in case the method or syntax I used is wrong.
In gnome-terminal,
--go to Edit -> Profiles.
--Double click on Default profile
--Click on Title And Command tab.
--Select Hold Terminal Opens from when command exits list which located at bottom of window.
--close windows
Now run your script it will work
Or you can also add your profile and run script as follows
Click the New tab. and give your profile name
Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command
To make the terminal stay when the command exits:
In konsole there is a --noclose flag.
In xterm, there is a -hold flag.
I've created an alias in .bashrc file as follows
alias myproject = 'cd ~/Desktop/myproject'
After saving the file when I restart my terminal, typing in myproject takes me to the project directory but when I try to use the alias as a command argument to a new gnome-terminal tab it throws an error,
gnome-terminal --tab -e "myproject"
throws the error
There was an error creating the child process for this terminal
Failed to execute child process "myproject" (No such file or directory)
What is wrong with this ?
When a bash shell is started, per default bash executes the commands specified in .bashrc. This is how your shell knows your aliases.
Now your idea does not work because gnome-terminal never sees your .bashrc file.
You could try
gnome-terminal --working-directory='<path-to-your-home-directory>/Desktop/myproject/
I was trying to do something similar... possibly not exactly what you want, but:
alias startMyRailsProject='cd ~/Desktop/myproject; gnome-terminal --tab --tab -e "rails s" --tab -e "rails c"; exit'
This:
- changes directory to where I want
- starts a new gnome terminal (in the right directory from before)
- creates a 2nd tab and starts my rails server
- creates a 3rd tab and starts my rails console
- and then closes the original terminal window which I call it from.
It does what I need it to and saves a bunch of repetive keystrokes :-)
Cheers
I succeeded in getting some of it to work, I am missing my aliases, but I can run the program I want in the following way:
gnome-terminal --window --title="testtitle" -- $SHELL -c "<path to script/application>/<script/application> <arguments>;"
An example:
gnome-terminal --window --wait --title="testtitle" -- $SHELL -c "echo test;read -p \"press any key to exit\" -n 1 ;"