help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Detecting shell type (ie: is/non interactive/ssh/login shell


From: Matthew Giassa
Subject: [Help-bash] Detecting shell type (ie: is/non interactive/ssh/login shell)
Date: Fri, 09 Sep 2016 10:56:19 -0700
User-agent: Workspace Webmail 6.4.8

Good day,

I'm working on a modified (GPL'ed of course) version of guake and
konsole to provide some additional UX features depending on what type of
shell is currently being used. IE: when in interactive mode, a green
light icon is illuminated; while running a script or lengthy command via
"bash -c" an amber light icon is lit; if a remote session is launched in
a special manner, other features kick in, etc.

The parameters I am primarily concern with is the type of shell being
launched, ie:

* Interactive or not (ie: "bash" versus "bash -c" or ". test.sh").
* Login shell or not.
* SSH-initiated (ie: remote) shell or not.

I am able to calculate these values via the following simple script,
which I've put in /etc/bash.bashrc on my Ubuntu test system:

    #!/bin/bash
    BASH_ENV=/etc/bash.bashrc # Needed so the non-interactive shells
(ie: bash -c) load this from
                              # a normal interactive shell without
having to manually specify the
                              # environment variable each time.
    if [[ $- == *i* ]]; then
        echo "interactive"
    else
        echo "non-interactive"
    fi
    
    if shopt -q login_shell ; then
        echo "login"
    else
        echo "non-login"
    fi
    
    # Detect whether or not this is an SSH-controlled session.
    if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
        echo "ssh"
    else
        case $(ps -o comm= -p $PPID) in
        sshd|*/sshd)
                echo "ssh"
                ;;
        *)
                echo "non-ssh"
                ;;
        esac
    fi
    

I was originally planning on having the modified guake/konsole
application detect a special string which would be generated by the
above script, but that approach appears to be doomed to failure as the
extra output generated for non-interactive shells will break sftp, scp,
etc. I've also looked into using bash-preexec
(https://github.com/rcaloras/bash-preexec), but using it in
non-interactive shells is risk prone, as noted by the author
(https://github.com/rcaloras/bash-preexec/issues/25).

Within the BASh source itself, which I've just started diving in to, is
there any way to query all three of the above values prior to calling
`execute_command` (execute_cmd.c)? If so, I could either provide a
modified BASh or use LD_PRELOAD to wrap the call and send data over a
pipe to the guake/konsole application to know what type of shell is in
use.

Thank you.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]