Bash / open a terminal with a command to run passed as an argument - bash

I created a script that starts all the apps I need for my day and assign them to the workspaces I want.
I'd like to know is there was a way to pass an argument to the terminal I open. The argument would be an alias that runs a massive source update of all the projects I'm working on.
I'd like to do something like this:
gnome-terminal start_rolls
Unfortunately, this only opens the terminal and the command is simply not run.
Any ideas ?
Thanks in advance !
Rolf
P.S.: the -e option does not work with aliases, it seems...

gnome-terminal --help-terminal-options says:
-e, --command Execute the argument to this option inside the terminal

some combination of "-e", "bash -c", "bash -i" and your alias might help

-e is going to be deprecated in future releases you should use something like this:
gnome-terminal -- bash -c "vim Main.java"
you can replace "vim Main.java" for whatever you want

Related

Sometimes I'd like to know what my shells aliases/functions are before I execute them. Can I log them without executing them?

I'm not sure if this is a stupid question but I'll often run into the sinario were I don't know what my commands are set to are without having to open my .zshrc (I'm using zsh obviously).
For instance lets use gs. I'm not sure which of the following gs will execute:
alias gs="git status"
or alias gs="gulp serve"
or alias gs="grunt serve"
Is there a command, or something like echo gs, "$(gs)" or echo "$(gs)" that will log the command (e.g. git status) rather executing what ever I have gs set to?
You can run the alias command to print out all of your shell aliases. If you want to know what a particular alias resolves to, just pass it to the alias command as an argument:
➜ alias gs
gs='git status'

Can't start script via screen command

I have a script, and I want to make it work in background after boot. Therefore, I added the command below into rc.local. However, it doesn't work.
sudo -iu executerroot screen -dmS test bash -c "bash /home/pi/FileServer/Run; exec bash"
Also I've tried commands in https://askubuntu.com/questions/261899/run-a-screen-session-on-boot-from-rc-local site but no one worked for me. If I execute
sudo -iu executerroot screen -dmS test bash -c "bash /home/pi/FileServer/Run; exec bash"
I get this:
bash: /home/pi/FileServer/Run/: No such file or directory
while it doesn't work for real files. When I type "screen -r" I get just blank bash screen if a file exists.
I can't figure out why it doesn't work. Please explain why it happens and how can I solve this.
Thanks...
After years, I got a similar problem today. This time I was trying to call the screen program inside crontab. I realized I needed absolute paths and correct bash version in crontab as well as in the script itself.
I think I had done a similar mistake 6 years ago. I wrote similar program today, probably, due to different environment variables, script didn't be able to find programs (in my case openvpn). Even program &> output.txt didn't work. The problem was the different bash version not supporting output redirection. I used /usr/sbin/bash -c "command" to cope with it. Also, openvpn is not in the path in that crontab environment, so I had to call it with absolute path (like /usr/sbin/openvpn). Then, it worked.

How to create a shell script to launch 3 terminals and execute a set of commands in each?

Currently, I open 3 terminals ('openocd session','telnet session' & 'gdb session') and execute 'a set of commands' in each terminal every time I flash my image on target.
Is is possible to put this in one shell script file and run at once? (Commands on each terminal are NOT dependent on others; except that the terminals should be opened in the order mentioned above)
Thanks in advance !
Which terminal are you using?
A terminal like xterm can start a program instead of a shell. Simply run
xterm -e 'command args ...'
I use this to start my email client:
xterm -e mutt
If you use a different terminal, consult its manual page. I'm pretty sure there's an equivalent for -e.

Print a cowsay in a new terminal

I want to run a script called tuxsay that combines fortune and cowsay to have tux say some words of wisdom. I made an alias in by .bash_aliases calling it tuxsay and executing the script
FORTUNE= $(fortune)
cowsay -f tux $FORTUNE
I then have a script that will spawn a new terminal and I want it to output the results of my first script then allow me to work in the terminal, something like
xterm -e "tuxsay&"
So how can I spawn a new terminal use cowsay then allow me to work in it?
Specifics: I have a startup script that will spawn applications that I typically use, and I want it to spawn a terminal that runs my tuxsay script then allows me to work. I am on Ubuntu 12.04 LTS and am running dwm 6.0
Why not just add your command to your .bashrc file? Then it will be run every time a new interactive shell is started. You can test if the $TERM variable contains "xterm", and make your output conditional upon that. e.g. add the following line to the end of .bashrc:
[[ "$TERM" =~ xterm ]] && tuxsay
Update
While I think the above is still probably the most useful general answer, I noticed a few more things:
tuxsay is a bash alias. bash aliases are only understood within bash itself, but when you pass a command to xterm -e, that command is executed directly by the xterm without any bash involvement. The system won't be able to find and execute a file called "tuxsay" and will fail.
If executed from bash, the & character will put tuxsay command (alias) in the background, but this doesn't really help here. What you need is a new shell to start up once tuxsay is done.
Be careful with .bash_aliases. I noticed on my Redhat-based system that aliases placed in this file were not working, because .bash_aliases file is not sourced in. (My Bash aliases don't work) So it looks like .bash_aliases is sourced by default from Ubuntu-based systems, but no guarantees for others.
With the above in mind, if you definitely want tuxsay to run on a per-xterm basis – i.e. you want to specify this on the xterm command line – you could do the following one-liner:
xterm -e 'cowsay -f tux $(fortune); bash'
This passes the compound command cowsay -f tux $(fortune); bash to xterm. This compound command consists of two commands: first do the cowsay thing, then start bash.
You could also put these two commands in a shell script and pass the shell-script to xterm.

running tmux automatically when xterm is launched: what is the most elegant/correct solution?

I am trying to setup my workstation in such a way that tmux is run for each terminal (xterm, gnome-terminal, ...) that is launched. I was thinking to add tmux to the .bashrc; problem is that if I launch bash twice for whatever reason, it start a second tmux inside the current tmux.
So:
is there a way to detect, maybe from the .bashrc, that the current bash is the 'first' one and not a second one launched in the same terminal?
any other good ideas / best practices / bash design patterns?
You can add the following to your .bash_profile:
SHELL=tmux
This is the first place xterm checks for the command to run if none is given on the command line.
Combine the two previous answers:
alias xterm='SHELL=tmux xterm'
You get the desired behavior when starting just xterm, but you can still use xterm for other operations, such as xterm top.
What about aliasing xterm to xterm tmux?
Just add the following line to your .bashrc:
alias xterm='xterm tmux'

Resources