fix history and ssh
authorIan Kelling <ian@iankelling.org>
Tue, 13 May 2014 11:44:29 +0000 (04:44 -0700)
committerIan Kelling <ian@iankelling.org>
Thu, 4 May 2017 23:40:14 +0000 (16:40 -0700)
.bashrc

diff --git a/.bashrc b/.bashrc
index ae0da0ae4d11bce5a50a3cde6c35899179571c02..f7af595af12ffc90f6cdd55acacfe51585f37510 100644 (file)
--- a/.bashrc
+++ b/.bashrc
@@ -9,17 +9,22 @@
 # Normally, this file is not sourced when a script is run, and it would be much
 # better and more consistent if that also happened when when running a script
 # over ssh. so here we test for conditions of a script under ssh and return if
-# so.  we can override with ssh -t which sets $SSH_TTY, which we can detect
+# so. we can override with ssh -t which sets $SSH_TTY, which we can detect
+# But inside a script, ssh -t won't work, because we aren't using a tty at all.
+# So we need something else. Command lines and env variables sent across ssh are strictly limited.
+# We could override an obscure unused LC_var, like telephone, or we could transfer a file.
+# But I choose to set SendEnv and AcceptEnv ssh vars for BASH_LOGIN_SHELL.
+
+[[ $- != *i* && ! $SSH_CONNECTION ]] && export BASH_LOGIN_SHELL=true
 if [[ $SSH_CONNECTION ]] \
    && [[ $- == *c* ]] \
    && [[ ! $SSH_TTY ]] \
+   && [[ ! $BASH_LOGIN_SHELL == true ]] \
    && [[ $- != *i* ]]; then
     return
 fi
 
-# Side note on ssh. Command lines and env variables sent across ssh are strictly limited.
-# If we did want to easily pass info, we could override an obscure unused LC_var
-# Or we could set SendEnv and AcceptEnv ssh vars, or we could transfer a file.
+# Side note on ssh.
 
 
 
@@ -88,10 +93,6 @@ if [[ $INSIDE_EMACS ]]; then
     export MANPAGER=cat
     # for readline-complete.el
     stty echo
-else
-    # terminal keys: C-c, C-z. the rest defined by stty -a are, at least in
-    # gnome-terminal, overridden by bash, or disabled by the system
-    stty werase undef lnext undef stop undef start undef
 fi
 
 
@@ -110,6 +111,10 @@ if [[ $- == *i* ]]; then
             bind '"\eOc": shell-forward-word'
             bind '"\eOd": shell-backward-word'
         fi
+        # terminal keys: C-c, C-z. the rest defined by stty -a are, at least in
+        # gnome-terminal, overridden by bash, or disabled by the system
+        stty werase undef lnext undef stop undef start undef
+
     fi
 
 fi
@@ -717,17 +722,17 @@ complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
 
 
 hl() { # history limit. Write extra history to archive file.
-    local max_lines linecount tempfile
+    local max_lines linecount tempfile prune_lines
     local harchive="${HISTFILE}_archive"
     for x in "$HISTFILE" "$harchive"; do
         if [[ ! $x || ! -w $x || $(stat -c "%u" "$x") != $EUID  ]]; then
-            echo "error in hl: history file $x no good."
+            echo "error in hl: history file $x no good"
            return 1
         fi
     done
-    history -w
+    history -a # save history
     max_lines=$HISTFILELINES
-    [[ $max_lines ]] || { echo "error in hl: failed to get max line count"; return 1; }
+    [[ $max_lines =~ ^[0-9]+$ ]] || { echo "error in hl: failed to get max line count"; return 1; }
     linecount=$(wc -l < $HISTFILE) # pipe so it doesn't output a filename
     [[ $linecount =~ ^[0-9]+$ ]] || { echo "error in hl: wc failed"; return 1; }
     if (($linecount > $max_lines)); then