run a looping command in 10 terminals - bash

an easy question from some of you.
I need to run a command which take into account 10 different files. For some reasons I need to run this command for each file in parallel possibly in 10 different terminals.
Is there a way to do this in a shell script? Ideally, I would open terminals in a loop i=10 then loopin into the files folder, however I do not know hot to open different terminals in a loop and assign a command to each one.
I hope it is clear, if not let me know

There are better ways to solve your problem, but if for some reason just want to open 10 terminals and in "parallel" run a command within them, you could give a try to tmux with the option synchronize-panes on, you could use this script for example:
#!/bin/sh
for i in {1..10}
do
tmux split-window
tmux select-layout tiled
done
clear
tmux select-layout tiled
tmux setw synchronize-panes on
After having tmux running just run it, it should look than something like the attached picture:

Related

How can I tell my bash prompt to indicate whether there's a backgrounded emacsclient session exists

I'm using, OS X, and mainly terminal and emacsclient.
When I do shell stuff, I background my emacsclient with Control-Z
Someties I forget whether i've done that, and end up spawning additional emacsclient sessions, which I don;t want to do.
It would be cool if the bash prompt can tell me whether emacsclient jobs up in the jobs output
Minimal example for bash, using sleep instead of emacsclient.
PS1="\`if jobs | grep -q sleep; then echo 'sleep jobs' ; else echo 'no sleep jobs' ; fi\`\\\$ "
You might want to filter on stopped jobs (jobs -s).
You can get fancier by echoing escape sequences instead of just strings to colorize it.
While I think #jpkota provides a workable answer, I wonder if maybe your worrying too much. Provided emacsclient is working OK, there is no problem with having multiple emacsclient sessions running at once - in fact, it is sort of designed to do that. The emacsclient connections are light-weight and if there is a chance you might need to use the same file/buffer again, you may as well keep them around and just open new ones when needed and get rid of the ones you don't think you will need. The whole benefit of emacscleint is that opening new windows/buffers is really fast and if you use the GUI verison, they just pop up in their own window.
There is also a package in elpa which may be useful called osx-pseudo-daemon, which addresses a problem that can occur if you close all emacsclient windows which prevents the main emacs from responding (this is when yu run emacs from launchctl.
What I tend to do is run emacsclient in GUI mode rather than inside a terminal. When I run emacsclient I put it in the background so that it doesn't block my terminal and use the -c flag.(I actually have a shell script which makes this easy - see http://emacsformacosx.com/tips for some ideas. I leave the emacsclient window open and just switch to it if I need to do some emacs editing etc.

Open a small window while in Vim for executing commands

I need to write lot of code and compile very often. I hate switching back and forth various windows just to compile the code. Is it possible to open a small window at bottom and run invoke shell and close that window when needed?
With GVim or MacVim, you can run external commands in the command-line: Gvim/MacVim comes with a (very) limited shell that will happily show you whatever the compiler outputs. The general usage pattern is:
:!command
:!command %
With CLI Vim, the same method will pause Vim and return to the shell to execute your command.
In both cases, you'll get a message asking you to press ENTER to come back to your normal editing.
Using :make | cw would be a slightly more sophisticated alternative, with the added bonus of showing the errors in the quickfix window.
An even more sophisticated approach would be to use Tim Pope's Dispatch plugin in combination with tmux or screen.
Sounds like a problem for Screen
http://www.gnu.org/software/screen/
Quick reference of commands
http://aperiodic.net/screen/quick_reference
I use tmux to achieve something like that. I have the following in my ~/.tmux.conf file:
bind s splitw -v -p 25 -c '#{pane_current_path}' '/bin/bash'
bind q kill-pane
On pressing Ctrl-b + s (prefix + s), a new pane containing a bash shell opens up at the bottom. I can run shell commands from there: find, grep, make, etc. When I'm done, I press Ctrl-b + q to close the shell.
To enable tmux on every bash session, add the following to your ~/.bashrc:
[[ -z "$TMUX" ]] && exec tmux
Maybe map a key to shell out to the compiler and run the program if compilation is successful:
:map F8 :!cc % && ./a.out
Or maybe just
:sh
make run
Ctrl-D
Another option is to suspend vi, using Ctrl-Z and do your stuff in the shell, then type fg to bring vim back to the foreground. Note that this is actually a feature of your shell, rather than vim but it produces the effect you seek.
Note this idea originates from the book "Efficient Linux at the Command Line" by Daniel Barrett. I forget the page number.

tmux bind-key to kill all or maybe a grouping of windows/panels

I've created a script that opens up tmux and splits into 4 windows/panels, but i was wondering if theres a quick way to exit them all, preferably via a key binding.
Thanks.
[Edit]
What i'm looking for is a way to exit a selection of windows via a key binding.
Something maybe i can add to my .tmux.conf file like:
bind-key C-c exit-all
Or maybe a command i can pass in after setting up the split windows etc like: tmux bind-key C-c exit-all
I'm putting exit-all as the command there as thats what im looking for, but i don't know the real name for this function or way of doing this
From man tmux:
unlink-window [-k] [-t target-window]
(alias: unlinkw)
Unlink target-window. Unless -k is given, a window may be unlinked only if it is linked to multiple ses-
sions - windows may not be linked to no sessions; if -k is specified and the window is linked to only one
session, it is unlinked and destroyed.
I think unlink-window -k is what you're after.

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'

Linux equivalent of the DOS "start" command?

I'm writing a ksh script and I have to run a executable at a separate Command Prompt window.
xdg-open is a similar command line app in linux.
see https://superuser.com/questions/38984/linux-equivalent-command-for-open-command-on-mac-windows for details on its use.
I believe you mean something like xterm -e your.sh &
Don't forget the final &
maybe it´s not a seperate window that gets started, but you can run some executables in background using "&"
e.g.
./myexecutable &
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for.
regards
xdg-open is a good equivalent for the MS windows commandline start command:
xdg-open file
opens that file or url with its default application
xdg-open .
opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
screen -ls
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
screen -x title
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this:
xterm yourtextmodeprogram
or
gnome-terminal -e yourtextmodeprogram
or
konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this:
batch-oriented: nohup command-to-run &
interactive: screen
if you want a new windows, just start a new instance of your terminal application: in kde it's
konsole -e whatever
i'm sure the Gnome terminal has similar options
Some have recommended starting it in the background with &, but beware that that will still send all console output from the application you launch to the terminal you launched it from. Additionally, if you close the initial terminal the program you loaded will end.
If you're using a desktop environment like KDE or GNOME, I'd check the alt+f2 launching apps (gnome-open is the one for GNOME, I don't know the name of the KDE app) and see if you can pass them the command to launch as an argument.
Also, if your intention is to launch a daemon, you should check the nohup documentation.
I used nohup as the following command and it works:
nohup <your command> &
then press enter and enter!
don't forget the last &
for example, I ran a python code listening to port 5000:
nohup python3 -W ignore mycode.py &
then I made sure of running by netstat -tulnp | grep :5000 and it was ok.

Resources