bashスクリプトを使用して端末を開いて使用する方法は?

bashスクリプトを使用して端末を開いて使用する方法は?

4つの端末を開く必要があります。各端末は4つの異なるリモートコンピュータに接続され、そのコンピュータの特定のディレクトリの場所を開きます。各端末は、マイコンピュータデスクトップ画面の別の場所で開きます。 Ubuntu-12.04を使用しています。

ベストアンサー1

一般的に好ましい方法の1つは、tmux背中を使用することです。screenこれがオプションではない場合 - 複数のターミナルウィンドウなどが必要な場合は、より洗練された方法でこれを実行できます。

これは見苦しく、よりよく解決することができますが、出発点として機能します。これはbashベースで、wmctrl端末ウィンドウの位置を指定するために使用されます。

1.端末ウィンドウを配置します。

私はターミナルウィンドウのサイズと位置を調整するためにクイックマッシュアップ(醜いスク​​リプト)を使いますwmctrlc1つまたは2つのオプションのパラメータを受け入れる関数にそれを入れました。これは人々のニーズに応じてハッキングすることができます。

c()
{
    local -i tbar_height=23  # To take title bar into account.
    local win r="-ir"
    local -i w h w2 h2 y2

    # Argument 2 is Window ID, x or none.
    if [[ "$2" =~ ^0x[0-9a-f]+$ ]]; then
        win="$2"
    elif [[ "$2" == "x" ]]; then
        win="$(xprop -root \
            -f _NET_ACTIVE_WINDOW \
            0x "\t\$0" _NET_ACTIVE_WINDOW | \
            cut -f2)"
    else
        win=":ACTIVE:"
        r="-r"
    fi

    # Get monitor size (fear this easily can bug – should be done better).
    read -d ', ' w h <<< \
        $( awk 'NR == 1 { print $8, $10; exit }' <(xrandr) )

    ((w2 = w / 2))                # Monitor width  / 2
    ((h2 = h / 2 - tbar_height))  # Monitor height / 2 - title bar
    ((y2 = h / 2))                # Monitor height / 2 - title bar

    case "$1" in #             g,   x,   y,   w,  h
    "nw") wmctrl $r "$win" -e "0,   0,   0, $w2, $h2";;
    "ne") wmctrl $r "$win" -e "0, $w2,   0, $w2, $h2";;
    "se") wmctrl $r "$win" -e "0, $w2, $y2, $w2, $h2";;
    "sw") wmctrl $r "$win" -e "0,   0, $y2, $w2, $h2";;
    "n")  wmctrl $r "$win" -e "0,   0,   0,  $w, $h2";;
    "e")  wmctrl $r "$win" -e "0, $w2,   0, $w2,  $h";;
    "s")  wmctrl $r "$win" -e "0,   0, $y2,  $w, $h2";;
    "w")  wmctrl $r "$win" -e "0,   0,   0, $w2,  $h";;
    "mh") wmctrl $r "$win" -e "0,  -1,   -1,  -1, $h";;
    "mw") wmctrl $r "$win" -e "0,  -1,   -1,  $w, -1";;
    esac
}

窓を北西、c nw北東c neなどに配置します。パラメータmhmwはそれぞれ最大高さと最大幅です。ウィンドウIDは引数2に渡されるか、"x"スクリプトから読み取ることができます。xpropそれ以外の場合は使用されます。:ACTIVE:

2.スクリプトの書き込み初期化Bashセッションは新しい端末ウィンドウで開き、場所を要求してSSH(またはその他)を開始します。

ホストをパラメータなどとして使用するようにここで調整できます。

#!/bin/bash

# Source positioning script.
. "$HOME/scripts/bash/_mypos_wmctrl"

# Position window according to argument 1.
c "$1"

# Act according to argument 2.
case "$2" in
"host1") ssh -t user@host1 "cd www; bash";;
"host2") ssh -t user@host2 "cd mail; bash";;
"host3") ssh -t user@host3 "cd dev; bash";;
"host4") ssh -t user@host4;;
esac

# Add this if one want to keep terminal window open after exit from ssh
/bin/bash

3.ランチャースクリプト。

wmctrlウィンドウIDを取得してアクションを実行するには、睡眠が必要です。チェックアウトxtoolwaitしたり、似たようなものが欲しいかもしれません。

#!/bin/bash

terminal=some-terminal-emulator

$terminal -e '/path/to/script2 "nw" "host1"'
sleep 1
$terminal -e '/path/to/script2 "ne" "host2"'
sleep 1
$terminal -e '/path/to/script2 "se" "host3"'
sleep 1
$terminal -e '/path/to/script2 "sw" "host4"'
sleep 1

長い間忘れられたスクリプトがありましたが、ウィンドウIDをtmpファイルに保存しました。このスクリプトは、すべての端末を別のデスクトップに移動したり、すべての端末を集中したり混合したりするなど、他のスクリプトから呼び出すことができます。

wmctrlほとんどのエミュレータ(および必要に応じて他のアプリ)で動作します。

現時点では、---^(提供されたスクリプトの例)はかなり見苦しいですが、おそらくこれらのいくつかに基づいて使用することができます。

おすすめ記事