byobu命令的shell脚本

时间:2017-04-27 15:38:28

标签: shell scripting byobu

需要编写一个shell脚本,使用单独的选项卡打开byobu终端。第一行打开新的byobu会话,后续行连接到该会话并打开新标签。它的自动化开放终端。

前 -

byobu new-session -s "Server" "redis-server"

byobu new-window "redis-cli"

byobu new-window "sudo mongod --port 27017 --dbpath /data/db/rs0 --replSet rs0"

byobu new-window "mongo"

这里的问题是当我运行这个shell脚本时它只运行第一个命令然后停止。如果我再次运行它,那么它将使用以下消息执行剩余的行:

  

重复会话:服务器

我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

我认为你错过了shell脚本的第一行。看看是否有效

#!/bin/sh
# byobu_launcher.sh ver 20170915122301 Copyright 2017 alexx, MIT Licence ver 1.0

byobu new-session -d -s $USER

# redis window
byobu rename-window -t $USER:0 'redis-cli'
byoby send-keys "redis-cli" C-m
byobu split-window -v

# mongod
byobu new-window -t $USER:1 -n 'mongod'
byobu send-keys "sudo mongod --port 27017 --dbpath /data/db/rs0 --replSet rs0" C-m

# mongo
byobu new-window -t $USER:1 -n 'mongo'
byobu send-keys "mong" C-m

# Set default window as the dev split plane
byobu select-window -t $USER:1

# Attach to the session you just created
# (flip between windows with alt -left and right)
byobu attach-session -t $USER

使用屏幕,您可以执行此操作添加到〜/ .screenrc

的末尾
screen -t redis-cli 0
stuff "redis-cli\n"
screen -t mongod 1
stuff "sudo mongod --port 27017 --dbpath /data/db/rs0 --replSet rs0\n"
screen -t mongo 2
stuff "mongo\n"
select 1

我主要使用屏幕,有时使用tmux。我没有使用过。[/ p>

答案 1 :(得分:0)

我前一阵子写了这个,也许对你有帮助 几乎没有注释,但是函数有明显的名称

#!/bin/bash

# create terminals and rename




function extract_session() {
    arg=$1
    [[ ! "${line:0:1}" = "[" ]] && echo Not a session block && exit 1
    [[ ! "${line: -1}" = "]" ]] && echo Not a session block && exit 1
    echo "${arg:1:-1}"
}

function new_session(){
    sess=$1
    if [[ $(byobu has-session -t ${sess} &> /dev/null; echo $?) -eq 0 ]] 
    then
      echo Session ${sess} already exists
    else
      printf "Starting new session %s\n" "${sess}"
      byobu new-session -d -s ${sess}
    fi
}

function new_window(){
    win=$1
    printf "Window %s: %s\n" "${win_id}" "${win}"
    if [[ ${win_id} -eq 0 ]]
    then
    byobu rename-window -t ${win_id} ${win}
    else
    byobu new-window -n ${win} 
    fi
    byobu select-pane -t ${pane_id} -T ${win}
}

function new_pane(){
    pane_name=$1
    printf "\t%s\t%s %s\n" ${pane_id} "${pane_name}" "${split}"
    byobu select-window -t ${win_id}
    byobu split-window ${split}
    byobu select-pane -t ${pane_id} -T ${pane_name}
    
}

while read line
do
    if [[ "${line:0:1}" = "[" ]]
    then
    # begin SESSION
    SESSION=$(extract_session ${line})
    new_session ${SESSION}
    win_id=0
    else
    pane_id=0
    # create windows with panes
    for pane in ${line[@]}
    do
        if [[ "${pane:0:1}" = "|" ]]
        then
        # create horizontal pane
        pane=${pane: 1}
        split=-h
        else
        # create vertical pane
        split=-v
        fi
        if [[ ${pane_id} -eq 0 ]]
        then
        new_window ${pane}
        else
        new_pane ${pane}
        fi
        pane_id=$((pane_id+1))
    done
    win_id=$((win_id+1))
    printf "\n"
    fi
    
done <<EOF
[guake]
ipython
sam |sam
ROOT iROOT
scp
TDOD
vpn
[joplin]
[RTERM]
sam
ROOT
man
[xterm]
monitor | htop | net
ROOT
man
[LTERM]
sam
ipython
ROOT
man
[DEV]
iPython |tail |run
run2
help
auto-scp
django
django-srv
[ansible]
playbook
inventory
[SSH]
ssh
[SSH2]
ssh
[SSH3]
ssh

EOF

底部的部分是here文档,您可以在其中添加带有窗格的会话名称。 我还想添加一些应该执行的命令,但是我已经到了