Open terminal windows and execute custom commands in them? - linux

I just figured out how I can open a new terminal and immediately send it commands. The command I'm using on Linux Mint is "mate-terminal -x zsh -c '(stuff here) ; exec /bin/zsh". But, I hit a wall in regards to calling functions and aliases defined in my .zshrc file. Instead it says "zsh:1: command not found: ".

.zshrc file is only used for interactive shells, http://linux.die.net/man/1/zsh
Then, if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc
There is -i option ("Force shell to be interactive.") which may help you, try:
mate-terminal -x zsh -c '(stuff here) ; exec /bin/zsh -i'
or if you have zshrc commands in stuff:
mate-terminal -x zsh -ci '(stuff here) ; exec /bin/zsh -i'

Related

In Linux, when does the "bash" command source a ~/.bash_profile or ~/.profile file?

I am currently using ssh to access a linux computer. I use the command:
ssh -t user#hostaddress 'cd ~/Desktop && bash'
When I get there, I see that neither ~/.bash_profile nor ~/.profile are sourced. What are the rules surrounding when these are sourced in? The reason I call bash is because I am able to get terminal colors when I do bash (blue folders, etc) that I otherwise cannot get just by using ssh user#hostaddress.
You're not running bash as a login shell -- using bash -l should source .bash_profile. Otherwise you can use .bashrc.

Executing alias commands in newly opened Tabs

I have the following bash script in Linux Ubuntu which opens a new terminal with multiple tabs and in each tab it executes ssh command to access a remote router:
#!/bin/bash
gnome-terminal --tab -e "ssh root#172.16.17.4" --tab -e "ssh root#172.16.17.5"
In the bashrc file, I have included the definition of the following alias commands:
alias router4='ssh root#172.16.17.4'
alias router5='ssh root#172.16.17.5'
When I replace the full ssh command in the first script with these alias commands, each Tab gives me the following error:
There was an error creating the child process for this terminal
Failed to execute child process "router6" (No such file or directory)
How to solve this problem?
Note: When I execute the previous alias commands in a maually opened tab, they work perfectly.
You could try switching from an alias to a function in your .bashrc. According to bash's documentation, functions are better than aliases for most situations. In your situation, since you're not running bash as a login environment, it might work better. Add this to your .bashrc and comment out the alias:
router4() {
/usr/bin/ssh root#172.16.17.4
}
If that doesn't work on its own, you could try adding export -f router4 in your .bashrc after defining router4.
You need to execute it through bash:
gnome-terminal --tab -e "bash -c router4"

How to execute multiple statements in multiple terminals? (Linux, Bash)

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?

Konsole execute a script

konsole --noclose -e --rcfile /filepathtomyscript
I tried to execute the above commands because i want konsole can execute a list of commands with arguments such as
lftp
open...
login
put....
However, I keep get errors. Could you please help me? Thanks a lot.
All the examples I found so far just execute a single command.
Try this
konsole --noclose -e /bin/bash /path/to/my/script
Konsole Man Page
-e [ arguments ]
Execute ’command’ instead of shell. It also sets the window
title and icon name to be the basename of the program being
executed if neither -T nor -n are given on the command line.
This must be the last option on the command line.

How to use GNOME Terminal instead of XTerm here?

I have the following variable defined in ~/.vimrc. This works well with XTerm but I can't get it working with GNOME Terminal. Please help.
let g:slimv_client = 'python /home/dumrat/.vim/ftplugin/slimv.py -r "xterm -e sbcl --core /home/dumrat/.sbcl/sbcl.core -s"'
The option -e makes XTerm run the command specified by all of the remaining
command line arguments following -e. Consequently, xterm -e sbcl --core
/home/dumrat/.sbcl/sbcl.core -s opens an XTerm instance running sbcl --core
/home/dumrat/.sbcl/sbcl.core -s command.
GNOME Terminal has the option -x with the same meaning that -e has for
XTerm.1 Thus, change the configuration file, as follows.
let g:slimv_client = 'python /home/dumrat/.vim/ftplugin/slimv.py -r "gnome-terminal -x sbcl --core /home/dumrat/.sbcl/sbcl.core -s"'
1 Note that -e has somewhat different behavior in GNOME
Terminal—the whole command is expected to be in the next argument, while -x
assumes that everything to the end is the command to run.

Resources