如何在 bash 脚本中编写以下内容?
tmux # Start tmux session.
compass watch /path/to/project1/compass/ # Run the first process.
Ctrl + B, " # Split the pane.
compass watch /path/to/project2/compass/ # Run the second process.
Ctrl + B, D # Exit the session.
Run Code Online (Sandbox Code Playgroud)
Kus*_*nda 32
tmux \
new-session 'compass watch /path/to/project1/compass/' \; \
split-window 'compass watch /path/to/project2/compass/' \; \
detach-client
Run Code Online (Sandbox Code Playgroud)
的new-session
(这将创建一个新的命令tmux
会话)和split-window
命令(其将当前窗口分成两个窗格)中tmux
采用任选的壳的命令来运行。最后detach-client
做的很明显。
如果您想要水平拆分(两个并排的窗格),请split-window -h
在上面的命令中使用。
tmux
向tmux
您发送多个命令时,需要用;
. 在;
需要通过引用/逸出它(被保护免受壳';'
,";"
或\;
),为了从将其解释为所述的端部挡块的外壳tmux
命令。
为了便于阅读,我已将整个内容分成单独的行。如果您在脚本中执行此操作(我建议这样做),请确保\
每行的 final 之后没有任何内容。
使用tmux a
、tmux attach
或重新附加到会话tmux attach-session
(这些都是等效的)。
tmux
一旦两个命令都执行完毕,会话将结束。
小智 11
这对我不起作用(我试图做类似“ls -la”的事情)。所做的是:
tmux new-session -d bash
tmux split-window -h bash
#sends keys to first and second terminals
tmux send -t 0:0.0 "<my-first-command>" C-m
tmux send -t 0:0.1 "<my-second-command>" C-m
tmux -2 attach-session -d
Run Code Online (Sandbox Code Playgroud)
这让我可以运行非常通用的东西,虽然它看起来很丑,但它非常实用。
只是把它留在这里以防其他人正在寻找同样的东西。
来源:https : //gist.github.com/kizzx2/4739236
小智 6
要运行一个简短的命令而不最终退出:
tmux \
new-session 'ls ; bash' \; \
split-window 'ls ; bash'
Run Code Online (Sandbox Code Playgroud)
或者
tmux \
new-session 'ls ; bash' \; \
new-window 'ls ; bash'
Run Code Online (Sandbox Code Playgroud)