如何在不同的目录中几个窗口开始TMUX? [英] How to start tmux with several windows in different directories?
问题描述
我想用一个脚本在不同的目录中有6个窗口,每个窗口打开TMUX会话。我开始用<一个href=\"http://blog.hawkhost.com/2010/07/02/tmux-%E2%80%93-the-terminal-multiplexer-part-2/#tmux-shell-scripting\">a剧本我发现并尝试这首:
I want to use a script to open a tmux session with 6 windows, each in a different directory. I started with a script I found and tried this first:
tmux new-session -s xyz -n etc -d 'cd /etc'
tmux new-window -t xyz:1 -n var 'cd /var/log'
但我很快发现,如我所料,这将无法正常工作 - shell命令完成后,该窗口将被关闭。
But I soon found out that this will not work as I expected — the window will be closed after the shell command completes.
所以,我的下一个想法是这样开始一个新的shell:
So my next idea was to start a new shell like this:
tmux new-session -s xyz -n etc -d 'cd /etc; bash -i'
tmux new-window -t xyz:1 -n var 'cd /var/log; bash -i'
tmux new-window -t xyz:2 -n var2 'cd /var/log; bash -i'
tmux new-window -t xyz:3 -n var3 'cd /var/log; bash -i'
tmux new-window -t xyz:4 -n var4 'cd /var/log; bash -i'
tmux new-window -t xyz:5 -n var5 'cd /var/log; bash -i'
tmux new-window -t xyz:6 -n var6 'cd /var/log; bash -i'
tmux select-window -t xyz:1
tmux -2 attach-session -t xyz
这几乎工程。但是,如果我开始比约4个窗口多了,我经常看到的一个窗口下面的bash错误启动后:
This almost works. But if I start more than about 4 windows, I frequently see the following bash errors in one of the windows after startup:
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
我不知道为什么会这样,但我仍然认为,我不是这样做的权利。有没有更好的方式来建立一个会话TMUX几个目录?
I have no idea why this happens, but I still think that I’m not doing this right. Is there a better way to set up a tmux session over several directories?
推荐答案
外壳错误可能是由于你的启动文件中的某些问题(或东西,他们运行)。
The shell errors are probably due to some problem in your startup files (or something they run).
由于shellter评论,暂时包括命令设置-vx
在启动过程的早期是找出了错误发生的好办法。结果
如果您发现 -vx
输出太详细,你可以试试的printf调试(手动添加调试语句来启动文件,直到你可以缩小到底是哪行引起错误):
As shellter commented, temporarily including the command set -vx
early in your startup sequence is a good way to find out where the errors are occurring.
If you find the -vx
output too verbose, you could try "printf debugging" (manually adding debug statements to your startup files until you can narrow down exactly which lines are causing the errors):
- 把
的.bashrc的回声启动
和在开始/结束的.bashrc
的回声结束你的的.bashrc
来看看在你的的.bashrc
出现错误。如果没有,你的仪表等启动文件:的.bash_profile
/.bash_login文件
/.profile文件
。如果错误的文件之前发生,那么问题可能出在/ etc / profile文件
。 - 一旦你知道哪些文件时发生的错误,左右各有重大块或行添加更多的调试输出来缩小负责部分/线路正在处理中。
- 的错误实际上可能不是你的启动文件本身,而是在它运行的脚本。
- Put
echo start of .bashrc
andecho end of .bashrc
at the start/end of your.bashrc
to see if the error occurs during your.bashrc
. If not, instrument your other startup files:.bash_profile
/.bash_login
/.profile
. If the errors happen before that file, then the problem may be in/etc/profile
. - Once you know which file is being processed when the errors occur, add more debug outputs around each "major block" or line to narrow down the responsible section/line.
- The errors may not actually be in your startup file itself, but in a script that it runs.
请注意:这些调试需要增加是暂时性的,因为它们会导致问题,如果你曾经使用一个程序,使自动登录(如的rsync 的,基于SSH的Git访问等),因为这些程序预计没有这样的调试噪音present一个干净的连接。
Note: These debug additions need to be temporary since they will cause problems if you ever use a program that makes automated logins (e.g. rsync, SSH-based Git access, etc.) since these programs expect a "clean" connection without such debugging noise present.
应该没有必要使用 CD
命令一样,在壳命令的给定参数为 TMUX新的会话
或 TMUX新窗口
。
There should be no need to use cd
command like that in the shell-command argument given to either tmux new-session
or tmux new-window
.
一个新的窗口将继承†当使用当前工作目录新会话
和新的窗口
在命令行(即当通过 TMUX
二进制,而不是通过做了结合,或在的 TMUX 的 - :
提示符)。根据变更的文件,它看起来像这样一直如此以来的 TMUX 的0.6(至少新窗口
)。
A new window will "inherit"† the current working directory when using new-session
and new-window
from the command line (i.e. when done through the tmux
binary, instead of via a binding or at a tmux-:
prompt). According to the CHANGES file, it looks like this has been the case since tmux 0.6 (at least for new-window
).
†这是 TMUX 的介导的继承,而不是父子的继承就是通常的机制沿CWD传递。
† This is tmux-mediated inheritance, not the parent–child inheritance that is the usual mechanism for passing along the cwd.
本脚本适用于我的 TMUX 的1.5:
This script works for me with tmux 1.5:
#!/bin/bash
# var for session name (to avoid repeated occurences)
sn=xyz
# Start the session and window 0 in /etc
# This will also be the default cwd for new windows created
# via a binding unless overridden with default-path.
cd /etc
tmux new-session -s "$sn" -n etc -d
# Create a bunch of windows in /var/log
cd /var/log
for i in {1..6}; do
tmux new-window -t "$sn:$i" -n "var$i"
done
# Set the default cwd for new windows (optional, otherwise defaults to session cwd)
#tmux set-option default-path /
# Select window #1 and attach to the session
tmux select-window -t "$sn:1"
tmux -2 attach-session -t "$sn"
这也说不定(作为副作用)减轻你的shell启动错误,因为路上的 TMUX 的启动shell是从一个普通的不同的bash -i
(它更像是的bash -l </ code>,后者使用
的.bash_profile
/ .bash_login文件
/ .profile文件
代替(只),你的的.bashrc
)
This might also (as a side-effect) alleviate your shell startup errors since the way tmux starts a shell is different from a plain bash -i
(it is more akin to bash -l
, which uses your .bash_profile
/.bash_login
/.profile
instead of (just) your .bashrc
).
这篇关于如何在不同的目录中几个窗口开始TMUX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!