Compare commits

..

No commits in common. "9c32ba4a8ce64b7dafc53f788c78170a60a530a3" and "0749f8ff07ae8f0f94b8a8572f03560797b3fa78" have entirely different histories.

3 changed files with 65 additions and 103 deletions

31
.gitignore vendored
View file

@ -1,31 +0,0 @@
### macOS template
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Backups
*~
*.bak

2
LDATE
View file

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
set -x set -x
date '+%Y-%m-%d %H:%M:%S.%N %z %Z' "$@" date '+%Y-%m-%d %H:%M:%S %z %Z' "$@"

View file

@ -1,25 +1,26 @@
#!/bin/bash #!/bin/bash
# DESCRIPTION # DESCRIPTION
# =========== # An attempt to seese industrial… ops, I ment an attempt to make my
# An attempt to seize industrial… ops, I ment an attempt to make my
# bash prompt nicer and save all my bash history ordered by date with # bash prompt nicer and save all my bash history ordered by date with
# exit codes for later review… Commands starting with space are not saved # exit codes for later review…
# because mc floods the logs otherwise.
# LICENSE: # LICENSE:
# Released under GNU GPL v 2+. You should've received it with your GNU/Linux # Released under GNU GPL v 2+. You should've received it with your GNU/Linux
# system. You can visit https://opensource.org/license/gpl-2-0/ or # system. You can visit https://opensource.org/licenses/gpl-2.0.php or
# alternatively write to the Free Software Foundation, Inc., # alternatively write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# z-ps-twtty-7.sh - Nice bash prompt and history archiver # KNOWN BUGS:
# by Doncho Gunchev <dgunchev@gmail.com>, 2024-01-27 15:58 EET # - logs the previous command again on control-break, control-quit
# Show PIPESTATUS instead of just $?. Cleanup. # - logs the last command in history on control+d (EOF) or enter
# Detect if BASH_COMMAND is an array (bash 5.1 NEWS) or just a string. # (could be fixed if we remember the index of the command used last time,
# Mark 'login' and 'logout's properly. # which is currently removed by 'Cmd=${Cmd:7}')
# I have 15+ years of bash history now, since 2009… # - does not log history from within mc (midnight commander) this can be
# # viewed as a feature too :-) The problem is that mc changes
# PROMPT_COMMAND, hurting the prompt quite bad and killing the history.
# Could be fixed on mc's side, no high hopes though (see RHBZ#1183192)
# ps-twtty-7.sh - Nice bash prompt and history archiver # ps-twtty-7.sh - Nice bash prompt and history archiver
# by Doncho Gunchev <dgunchev@gmail.com>, 2020-10-11 14:46 EET # by Doncho Gunchev <dgunchev@gmail.com>, 2020-10-11 14:46 EET
# Integrate better with vte.sh, just name this in a way to sort after it. # Integrate better with vte.sh, just name this in a way to sort after it.
@ -46,26 +47,33 @@
if [ "$PS1" ] ; then # interactive shell detection if [ "$PS1" ] ; then # interactive shell detection
# Log the logout event.
function prompt_command_exit() { function prompt_command_exit() {
trap - EXIT trap - EXIT
local now=$(date --rfc-3339=ns) # mark the logout
local HistFile local HistFile="$HOME/bash_history/$(date '+%Y-%m/%Y-%m-%d')"
HistFile="$HOME/bash_history/$(date '+%Y-%m/%Y-%m-%d')"
mkdir -p "${HistFile%/*}" mkdir -p "${HistFile%/*}"
echo -e "# Logout,$USER@${HOSTNAME}:$PWD,$(tty),${SSH_CLIENT:-local},login=${my_LoginTime:-$now},now=$now\nlogout" >> "$HistFile" local Cmd="$(history 1)"
#Cmd=$(echo "$Cmd" | sed 's/^ *[[:digit:]][[:digit:]]* *//')
Cmd="${Cmd:7}"
echo -e "# Logout,$USER@${HOSTNAME}:$PWD,$(tty),${SSH_CLIENT:-$(who am i | cut -d ' ' -f 1)@localhost},${my_LoginTime},$(date --rfc-3339=ns)\n$Cmd" >> "$HistFile"
} }
# Executed before each prompt. Fill the variables needed by PS1 here.
function prompt_command() { function prompt_command() {
local now=$(date --rfc-3339=ns) # Save the error code, running any external command resets it!
local E=$?
# Manage the history # Manage the history
local HistFile="$HOME/bash_history/$(date '+%Y-%m/%Y-%m-%d')" local HistFile="$HOME/bash_history/$(date '+%Y-%m/%Y-%m-%d')"
mkdir -p "${HistFile%/*}" mkdir -p "${HistFile%/*}"
if [ -z "$my_LoginTime" ]; then if [ -z "$my_LoginTime" ]; then
my_LoginTime="$now" my_LoginTime=$(date --rfc-3339=ns)
echo -e "# Login,$USER@${HOSTNAME}:$PWD,$(tty),${SSH_CLIENT:-$(who am i | cut -d ' ' -f 1)@localhost},${my_LoginTime},${my_LoginTime}\n" >> "$HistFile"
else
local Cmd="$(history 1)"
#Cmd=$(echo "$Cmd" | sed 's/^ *[[:digit:]][[:digit:]]* *//')
Cmd="${Cmd:7}"
echo -e "# CMD,\$?=$E,$USER@${HOSTNAME}:$PWD,$(tty),${SSH_CLIENT:-$(who am i | cut -d ' ' -f 1)@localhost},${my_LoginTime},$(date --rfc-3339=ns)\n$Cmd" >> "$HistFile"
fi fi
# Calculate the width of the prompt: # Calculate the width of the prompt:
@ -74,54 +82,42 @@ function prompt_command() {
my_PWD="${PWD}" my_PWD="${PWD}"
# Add all the accessories below ... # Add all the accessories below ...
my_D="$(date '+%Y-%m-%d %H:%M:%S')" my_D="$(date '+%Y-%m-%d %H:%M:%S')"
# This is for string size calculations only. local prompt="--($my_D, Err $E, $my_TTY)---($PWD)--"
local prompt="--($my_D, Err ${my_P[*]}, $my_TTY)---($PWD)--" local fillsize=0
local fill_size=0
[ -z "${COLUMNS}" ] && COLUMNS=$(tput cols) [ -z "${COLUMNS}" ] && COLUMNS=$(tput cols)
((fill_size=COLUMNS-${#prompt})) let fillsize=${COLUMNS}-${#prompt}
my_FILL="" my_FILL=""
if [ "$fill_size" -gt 0 ]; then if [ $fillsize -gt 0 ]; then
my_FILL="────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────" my_FILL="────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────"
while [ "$fill_size" -gt ${#my_FILL} ]; do while [ $fillsize -gt ${#my_FILL} ]; do
my_FILL="${my_FILL}${my_FILL}${my_FILL}${my_FILL}" my_FILL="${my_FILL}${my_FILL}${my_FILL}${my_FILL}"
done done
my_FILL="${my_FILL::$fill_size}" my_FILL="${my_FILL::$fillsize}"
fi fi
if [ "$fill_size" -lt 0 ]; then if [ $fillsize -lt 0 ]; then
my_PWD="${my_PWD:1-$fill_size}" my_PWD="${my_PWD:1-$fillsize}"
fi
local OldCmdNo="$CmdNo" # See if we got new command later.
local Cmd="$(history 1)"
CmdNo="${Cmd:0:7}"
if [[ -z "$OldCmdNo" ]]; then
Cmd="login"
else
Cmd="${Cmd:7}"
fi
if [ "$OldCmdNo" != "$CmdNo" ]; then # Only save new commands, not empty lines or Ctrl+C.
echo -e "# PIPESTATUS=${my_P[*]},$USER@${HOSTNAME}:$PWD,$(tty),${SSH_CLIENT:-local},login=$my_LoginTime,now=$now\n$Cmd" >> "$HistFile"
fi fi
# Let other PROMPT_COMMAND scripts (if any after us) have the error code too.
bash -c "exit $E"
} }
function twtty { function twtty {
# The special "\[" and "\]" are telling bash that the text enclosed will not move the caret. local GRAY="\[\033[1;30m\]"
local GRAY='\[\033[1;30m\]' local LIGHT_GRAY="\[\033[0;37m\]"
local LIGHT_GRAY='\[\033[0;37m\]' local WHITE="\[\033[1;37m\]"
local WHITE='\[\033[1;37m\]' local NO_COLOUR="\[\033[0m\]"
local NO_COLOUR='\[\033[0m\]'
local LIGHT_BLUE='\[\033[1;34m\]' local LIGHT_BLUE="\[\033[1;34m\]"
local YELLOW='\[\033[1;33m\]' local YELLOW="\[\033[1;33m\]"
local RED='\[\033[0;31m\]' local RED="\[\033[0;31m\]"
local LIGHT_RED='\[\033[1;31m\]' local LIGHT_RED="\[\033[1;31m\]"
local GREEN='\[\033[0;32m\]' local GREEN="\[\033[0;32m\]"
local LIGHT_GREEN='\[\033[1;32m\]' local LIGHT_GREEN="\[\033[1;32m\]"
if [ "${UID}" -ne '0' ]; then if [ "${UID}" -ne "0" ]; then
# Normal user colors # Normal user colors
local C1="${GREEN}" local C1="${GREEN}"
local C2="${LIGHT_GREEN}" local C2="${LIGHT_GREEN}"
@ -133,18 +129,19 @@ function twtty {
local C3="${WHITE}" local C3="${WHITE}"
fi fi
case "$TERM" in case "$TERM" in
xterm*) xterm*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]' TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;; ;;
*) *)
TITLEBAR='' TITLEBAR=""
;; ;;
esac esac
export PS1="$TITLEBAR\ export PS1="$TITLEBAR\
${C1}${C2}(\ ${C1}${C2}(\
${C1}\${my_D}${C2}, ${C1}Err ${C3}\${my_P[*]}${C2}, ${C3}\${my_TTY}\ ${C1}\${my_D}${C2}, ${C1}Err ${C3}\$?${C2}, ${C3}\${my_TTY}\
${C2})${C1}\${my_FILL}${C2}(\ ${C2})${C1}\${my_FILL}${C2}(\
${C1}\${my_PWD}\ ${C1}\${my_PWD}\
${C2})${C1}\ ${C2})${C1}\
@ -154,32 +151,28 @@ ${C1}\${USER}${C2}@${C1}\${HOSTNAME%%.*}\
${C2})${C3}\$${NO_COLOUR} " ${C2})${C3}\$${NO_COLOUR} "
export PS2="${C2}${C1}${C1}${NO_COLOUR} \[\033[K\]" export PS2="${C2}${C1}${C1}${NO_COLOUR} \[\033[K\]"
if [ -z "${PROMPT_COMMAND}" ]; then
local P='my_P=("${PIPESTATUS[@]}");prompt_command' PROMPT_COMMAND=prompt_command
if declare -p PROMPT_COMMAND &>/dev/null; then
local re='^declare -a '
if [[ "$(declare -p PROMPT_COMMAND)" =~ $re ]]; then # Array, supported since bash 5.1
PROMPT_COMMAND=("$P" "${PROMPT_COMMAND[@]}")
else # String
PROMPT_COMMAND="$P;${PROMPT_COMMAND}"
fi
else else
PROMPT_COMMAND="$P" if ! echo "${PROMPT_COMMAND}" | grep '\bprompt_command\b' > /dev/null 2>&1; then
PROMPT_COMMAND="prompt_command
${PROMPT_COMMAND}"
fi
fi fi
unset P
trap prompt_command_exit EXIT trap prompt_command_exit EXIT
shopt -s cmdhist histappend shopt -s cmdhist histappend
export HISTCONTROL='ignorespace' # ':erasedups' would prevent logging duplicate commands. export HISTCONTROL='ignorespace:erasedups'
export HISTIGNORE='history:history *' export HISTIGNORE='history:history *'
} }
# Secure bash history # Secure bash history
if [ ! -d "$HOME/bash_history" ]; then if [ ! -d "$HOME/bash_history" ]; then
mkdir -m 0700 "$HOME/bash_history" mkdir "$HOME/bash_history"
chmod 0700 "$HOME/bash_history"
fi fi
# call and unset twtty
twtty; unset twtty
unset twtty
fi fi