Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to open four terminals each will then connect to four different remote pcs and open specific directory locations on those pcs. Each terminal will be opened in different locations on my pc's desktop screen. How this can be done using bash scripts? I am using Ubuntu-12.04. This would be a great help because everytime doing this is a overkill to me.

share|improve this question
1  
You could use screen as your terminal "multiplexer" and launch 4 screens with different commands: stackoverflow.com/questions/10017058/… – nwildner Jun 12 at 0:58
Please also lookup this solution stackoverflow.com/questions/4260633/… – R.. Jun 12 at 1:03
add comment (requires an account with 50 reputation)

1 Answer

One, often preferred way would be to use tmux, screen or the like. If this is not an option – you want multiple terminal windows etc. you could go about it in a more complex way.

This looks ugly and can be solved better, but as a starting point. This is bash based using wmctrl to position terminal windows.

1. Positioning terminal windows.

I use a quick mash-up (ugly script) for resizing and positioning terminal windows using wmctrl. I have this in a function named c which takes one or optionally two arguments. This can be hacked to meet ones needs.

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
}

To position window north-west one say c nw, north-east c ne etc. Arguments mh and mw is max height and max width respectively. Window ID can be passed as argument two, or "x" for the script to read it from xprop – else use :ACTIVE:.

2. Script to initialize bash session in new terminal windows, request positioning, and start ssh (or whatever).

Here one can tune it to take host as argument etc.

#!/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. Launcher script.

Sleep is needed for wmctrl to get window ID and act upon it. You might want to look into xtoolwait or similar instead.

#!/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

Had a script once, long lost and forgotten, that also saved window ID's to tmp file which upon another script could be called to move all terminals to other desktop, raise all to focus, shuffle, etc.

Using wmctrl it should be usable with most emulators (and other applications if one want).

As it is now ---^, (the script samples provided), it is rather ugly, but you can perhaps use some of it as a base.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.