Executing alias commands in newly opened Tabs - linux

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"

Related

Programmatically open Gnome Terminal and run command

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.

Source in .sh script does not work after opening new terminal from .sh script

I have a problem with sourcing aliases. It's really specific case. I open new terminal window with two tabs using open_new.sh:
#!/bin/bash
gnome-terminal --tab --title="Tab1" -e "./tab1.sh" --tab --title="Tab2" -e "./tab2.sh"
In file tab1.sh i have:
#!/bin/bash
ls
. ~/.my_aliases
echo "done"
exec bash
File tab2.sh looks very similar. Line 2 and 4 works fine but it looks like line 3 does not work. When I try to use 'lll' alias it says 'bash: lll: command not found'. Then when I type command from line 3 directly in terminal aliases start working.
I have tried many different solution but I still can't handle it. For example I've tried something like this before sourcing:
shopt -s expand_aliases
I've tried this:
source ~/.my_aliases
And also I've tried:
#!/bin/bash -i
Is someone able to help me?
Maybe I should mention two things: I am not root user, I don't have any problems with sourcing that file with aliases in normal way.
Thanks in advance.
I've been looking for answer for three days before I asked question here. And now I've found the answer that I want to share if someone else will struggle with this problem in the future. In file open_new.sh:
#!/bin/bash
gnome-terminal --tab --title="Tab1" -e "bash --rcfile ./tab1.sh" --tab --title="Tab2" -e "bash --rcfile ./tab2.sh"
In file tab1.sh:
. ~/.bashrc
. ~/.my_aliases
ls
echo "done"
Explanation:
--rcfile File #execute commands from File instead of the standard personal initialization file ~/.bashrc
In File you can place many other commands. All bash commands worked for me. I've found solution in:
man bash
I suggest appending the following to your ~/.bashrc:
source ~/.my_aliases
This way, all new tabs will inherit your aliases.

Open terminal windows and execute custom commands in them?

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'

gnome-terminal new tab with alias as command to execute

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 ;"

Avoid gnome-terminal close after script execution?

I created a bash script that opens several gnome-terminals, connect to classroom computers via ssh and run a script.
How can I avoid that the gnome-terminal closes after the script is finished? Note that I also want to be able to enter further commands in the terminal.
Here is an example of my code:
gnome-terminal -e "ssh root#<ip> cd /tmp && ls"
As I understand you want gnome-terminal to open, have it execute some commands, and then drop to the prompt so you can enter some more commands. Gnome-terminal is not designed for this use case, but there are workarounds:
Let gnome-terminal run bash and tell bash to run your commands and then start a new bash
$ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
or if the commands are in a script
$ gnome-terminal -- bash -c "./scripttorun; exec bash"
The first bash will terminate once all the commands are done. But the last command is a new bash which will then just keep running. And since something is still running gnome-terminal will not close.
Let gnome-terminal run bash with a prepared rcfile which runs your commands
Prepare somercfile:
source ~/.bashrc
echo foo
echo bar
Then run:
$ gnome-terminal -- bash --rcfile somercfile
bash will stay open after running somercfile.
i must admit i do not understand completely why --rcfile has this behaviour but it does.
Let gnome-terminal run a script which runs your commands and then drops to bash
Prepare scripttobash:
#!/bin/sh
echo foo
echo bar
exec bash
Set this file as executable.
Then run:
$ gnome-terminal -- ./scripttobash
for completeness
if you just want to be able read the output of the command and need no interactivity
go to preferences (hamburger button -> preferences)
go to profiles (standard or create a new one)
go to command tab
when command exits -> hold the terminal open
i recommend to create a new profile for just for this use case.
use the profile like this:
gnome-terminal --profile=holdopen -- ./scripttorun
Every method has it's quirks. You must choose, but choose wisely.
I like the first solution. it does not need extra files or profiles. and the command says what it does: run commands then run bash again.
All that said, since you used ssh in your example, you might want to take a look at pssh (parallel ssh). here an article: https://www.cyberciti.biz/cloud-computing/how-to-use-pssh-parallel-ssh-program-on-linux-unix/
Finally this one works for me:
gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
Stack Overflow answer: the terminal closes when the command run inside it has finished, so you need to write a command that doesn't terminate immediately. For example, to leave the terminal window open until you press Enter in it:
gnome-terminal -e "ssh host 'cd /tmp && ls'; read line"
Super User answer: Create a profile in which the preference “Title and Command/When command exits” is set to “Hold the terminal open”. Invoke gnome-terminal with the --window-with-profile or --tab-with-profile option to specify the terminal name.
Run with -ic instead -i to make terminal close bash proccess when you close your terminal gui:
gnome-terminal -e "bash -ic \"echo foo; echo bar; exec bash\""
As of January 2020, the -e option in gnome-terminal still runs properly but throws out the following warning:
For -e:
# Option “-e” 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.
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
$ gnome-terminal -- "./scripttobash"
$ gnome-terminal -- "./genericscripttobash \"echo foo\" \"echo bar\""
I hope this helps anyone else presently having this issue :)
The ideal solution would be to ask for a user input with echo "Press any key".
But if double-click in Nautis or Nemo and select run in a terminal, it doesn't seem to work.
In case of Ubuntu a shell designed for fast start-up and execution with only standard features is used, named dash I believe.
Because of this the shebang is the very first line to start with to enable proper use of bash features.
Normally this would be: #!/bin/bash or similar.
In Ubuntu I learned this should be: #!/usr/bin/env bash.
Many workarounds exist to keep hold of the screen before the interpreter sees a syntax error in a bash command.
The solution in Ubuntu that worked for me:
#!/usr/bin/env bash
your code
echo Press a key...
read -n1
For a solution applicable to any terminal, there is a script that opens a terminal, runs the command specified and gives you back the prompt in that new terminal:
https://stackoverflow.com/a/60732147/1272994
I really like the bash --rcfile method
I just source ~/.bashrc then add the commands I want to the new startrc.sh
now my automated start.sh work environment is complete... for now 😼
If running a bash script just add gedit afile to the end of the script and that will hold gnome-terminal open. "afile" could be a build log which it was in my case.
Did not try just using gedit alone but, that would properly work too.
Use nohup command.
nohup gnome-terminal -e "ssh root# cd /tmp && ls"
Hope this will help you.

Resources