SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2014-02-24 15:53:09

wakatana
New Member
Registered: 2014-02-24
Posts: 4

[SOLVED] terminator+screen+bashrc+doublecmd

Hello All, my first post here, I am new to CrunchBang but not Linux. My $HOME/.bashrc starts with following line:

source ~/profile/dotfiles/.bashrc_screen

This file contains following code (comments and echos were added for debug purposes):

function scrr_attch_sess_tab_open() { screen -S "$attached_session" -X chdir "$@"; screen -X screen; }
function scrr_reattch_or_start_new() { (screen -rd "$attached_session" || screen -S "$new_session_name_err") && exit; }

if [ "$TERM" == "xterm" ]; then
  new_session_name_ok="ok_mode";
  new_session_name_err="err_mode";
  attached_session=$(screen -ls | grep 'Attached' | awk '{print $1}' | head -1);
  detached_session=$(screen -ls | grep 'Detached' | awk '{print $1}' | head -1);
  echo "A: $attached_session" >> /tmp/screen
  echo "D: $detached_session" >> /tmp/screen

  if [ "$attached_session" != "" ]; then
    echo "`date`: attached_session: $attached_session" >> /tmp/screen
    #scrr_attch_sess_tab_open "$(pwd)";
    #scrr_reattch_or_start_new;
  elif [[ "$detached_session" != "" ]]; then
    echo "`date`: detached_session: $detached_session" >> /tmp/screen
    #screen -r "$detached_session";
    #scrr_attch_sess_tab_open "$(pwd)";
    #scrr_reattch_or_start_new;
  else
    echo "`date`: else branch" >> /tmp/screen
    #screen -S "$new_session_name_ok" && exit;
  fi
fi

I have investigated that following two variables are always empty (no mather if I have detached/attached/existing/nonexisting screen session) and this causes that script always falls into elese branch:

$attached_session
$detached_session

I have tried to verify if my code is OK by creating the following "copy" of script and calling it. The result is that the code should be correct because when I issue this script from command line it works as expected:

#!/bin/bash
if [ "$TERM" == "xterm" ]; then
  new_session_name_ok="ok_mode";
  new_session_name_err="err_mode";
  attached_session=$(screen -ls | grep 'Attached' | awk '{print $1}' | head -1);
  detached_session=$(screen -ls | grep 'Detached' | awk '{print $1}' | head -1);
  echo "A: $attached_session"
  echo "D: $detached_session"

  if [ "$attached_session" != "" ]; then
    echo "1"
  elif [[ "$detached_session" != "" ]]; then
    echo "2"
  else
    echo "3"
  fi
fi

What is interesting is examination that, when I comment the lines which are appending to /tmp/screen (in first code) it also magically supress those two lines:

echo "A: $attached_session" >> /tmp/screen
echo "D: $detached_session" >> /tmp/screen

This is something that I cannot understand. Before CrunchBang I had Fedora and this script worked. The whole purpose of script is to have opened always only one instance of terminator and only one instance of screen but with "tab" inside screen which is open in aporoporiate path. On Fedora this worked that way, when i issued terminal from DoubleCmd it opened me new "tab" in screen which pointed to path from which I issued the terminal. Thank you for any idea.

Last edited by wakatana (2014-02-25 00:40:07)

Offline

Be excellent to each other!

#2 2014-02-25 00:38:32

wakatana
New Member
Registered: 2014-02-24
Posts: 4

Re: [SOLVED] terminator+screen+bashrc+doublecmd

Finally I got it. There was problem with broken pipe which seems to be solved by nawk and variable named session. I've also little bit update the script. Here it is, hope somebody will find it usefull  cool  feel free to comment glasses

############################################################################################################################################################
# OSATEAT 1.0 - One Screen And Terminal Emulator At Time
#
# Description:
# This script opens new screen's "tab" according actual DoubleCmd's panels paths (or according actual path from which terminal emulator is issued).
# if screen was not running - start it,
# if screen was detached - attach to it,
# if screen was attached in another window - detach/reatach to it.
#
# Summary:
# There will be always runing only one terminal emulator and only one instance of screen at given time.
# The only thing that will change are tabs in screen.
# Tthere will be always one terminal emulator icon on tint2
#
# TODO:
# - In dualhead (multi monitor) perserve the display where terminal emulator was open from
# - Fix accent directories problem
#
# Known Bugs:
# - Does not works with directories which contains accent
############################################################################################################################################################

if [ "$TERM" == "xterm" ]; then
  new_session_name_ok="ok_mode";
  new_session_name_err="err_mode"; # Hopefully this will be never used
  sessions=$(screen -ls 2>&1)
  attached_session=$(echo "$sessions" | grep 'Attached' | nawk '{print $1}' | head -1);
  detached_session=$(echo "$sessions" | grep 'Detached' | nawk '{print $1}' | head -1);
  
  if [ "$attached_session" != "" ]; then
    # 2.1 Open new tab according to $(pwd) variable
    screen -S "$attached_session" -X chdir "$(pwd)" && screen -X screen;
    sleep 1;
    # 2.2 Reatach to attached terminal. Old terminal emulator window exits and new terminal emulator window is created,
    # Again, if this newly created screen session will be detached/killed (ctrl+a+d or ctrl+a+k or ctrl+d) terminal also exits
    # Hopefully this will be never used: screen -S "$new_session_name_err" 
    (screen -rd "$attached_session" || screen -S "$new_session_name_err") && exit;
  elif [ "$detached_session" != "" ]; then
    # 3 Wait in background for 2 seconds until screen is connected to detached session and then use same commands as in 2.1
    sleep 2 && (screen -S "$detached_session" -X chdir "$(pwd)" && screen -X screen;) &
    screen -rd "$detached_session" && exit;
  else
    # 1. Start new screen session (when session will be detached or killed the terminal from which it was invoked also exits)
    screen -S "$new_session_name_ok" && exit;
  fi
fi

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo