【问题标题】:tmux split-window using shell script使用 shell 脚本的 tmux 拆分窗口
【发布时间】:2021-08-01 01:50:00
【问题描述】:

我正在尝试编写用于在 tmux 中创建窗格的 shell 脚本。

#!/bin/sh
tmux rename-window user
tmux new-session -d
tmux split-window -h
tmux selectp -t 1
tmux split-window -h
tmux select-layout even-horizontal
tmux selectp -t 2
tmux split-window -v

但是当执行上面的代码时并没有产生想要的输出。

图片表示代码执行时产生的输出:

当所有命令都写在 tmux 中时,它会产生所需的输出。

图片为手动输入命令时产生的输出:

如何修改代码以产生预期的结果?

【问题讨论】:

    标签: shell tmux


    【解决方案1】:

    试试这个,它看起来像你需要的(从“期望的输出”图像判断):

    tmux split-window -h
    tmux split-window -h
    tmux select-layout even-horizontal
    tmux split-window -v
    tmux select-pane -t 0
    

    您也可以尝试使用https://github.com/tmux-plugins/tmux-resurrect 之类的方式来保持您的布局。

    【讨论】:

    • 我试过了,但它没有提供所需的布局。当然,我会试试这个插件,但是有什么办法可以在 shell 脚本中做到这一点?
    【解决方案2】:
    #!/bin/bash
    
    ssh_list=( `cat $1` )
    split_list=()
    round=0
    i=0
    for ssh_entry in "${ssh_list[@]:1}"; do
            if (( $((round%2)) == 0 ))
            then
                    split_list+=( split-window -v -t $((2*i)) -p 50 ssh "$ssh_entry" ';' )
            else
                    split_list+=( split-window -h -t $((2*i)) -p 50 ssh "$ssh_entry" ';' )
            fi
            mxrd=$((2**round))
            i=$((i+1))
            if ((round==0)) || ((i>=mxrd))
            then
                    i=0
                    round=$((round+1))
            fi
    done
    
    tmux new-session ssh "${ssh_list[0]}" ';' \
        select-layout tiled ';' \
        "${split_list[@]}" \
        set-option -w synchronize-panes
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
    【解决方案3】:

    您可以在一次调用中执行所有tmux 命令:

    tmux new-session -d \; split-window -h \; split-window -h\; split-window -v
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2011-07-23
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多