web-dev-qa-db-fra.com

Est-ce que bash a un hook qui est exécuté avant d'exécuter une commande?

Sous bash, puis-je organiser une fonction juste avant d'exécuter une commande?

Il existe $Prompt_COMMAND, qui est exécuté avant d’afficher une invite, c’est-à-dire juste après l’exécution d’une commande.

$Prompt_COMMAND de Bash est analogue à la fonction precmd de zsh; donc ce que je recherche, c’est un équivalent bash de preexec de zsh.

Exemples d'applications: définissez le titre de votre terminal sur la commande en cours d'exécution; ajoute automatiquement time avant chaque commande.

106
Gilles

Pas nativement, mais il peut être piraté en utilisant le piège DEBUG. Ce code définit les fonctions preexec et precmd similaires à zsh. La ligne de commande est transmise sous la forme d'un argument unique à preexec.

Voici une version simplifiée du code pour configurer une fonction precmd exécutée avant l'exécution de chaque commande.

preexec () { :; }
preexec_invoke_exec () {
    [ -n "$COMP_LINE" ] && return  # do nothing if completing
    [ "$BASH_COMMAND" = "$Prompt_COMMAND" ] && return # don't cause a preexec for $Prompt_COMMAND
    local this_command=`HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"`;
    preexec "$this_command"
}
trap 'preexec_invoke_exec' DEBUG

Cette astuce est due à Glyph Lefkowitz ; merci à bcat pour localiser l’auteur original.

Modifier. Une version mise à jour du hack de Glyph peut être trouvée ici: https://github.com/rcaloras/bash-preexec

89
Gilles

Vous pouvez utiliser la commande trap (à partir de help trap):

Si SIGNAL_SPEC est DEBUG, ARG est exécuté avant chaque commande simple.

Par exemple, pour changer le titre du terminal de manière dynamique, vous pouvez utiliser:

trap 'echo -e "\e]0;$BASH_COMMAND\007"' DEBUG

De cette source.

18
cYrus

J'ai récemment eu à résoudre ce problème exact pour un projet parallèle. J'ai créé une solution assez robuste et résiliente qui imite les fonctionnalités preexec et precmd de zsh pour bash.

https://github.com/rcaloras/bash-preexec

Il était à l'origine basé sur la solution de Glyph Lefkowitz, mais je l'ai amélioré et mis à jour. Heureux d'aider ou d'ajouter une fonctionnalité si nécessaire.

11
RCCola

Ce n’est pas une fonction Shell qui est exécutée, mais j’ai fourni une chaîne d’invite $ PS0 à afficher avant l’exécution de chaque commande. Détails ici: http://stromberg.dnsalias.org/~strombrg/PS0-Prompt/

$ PS0 est inclus dans bash 4.4, mais la plupart du temps, la plupart des Linux doivent en inclure 4.4 - vous pouvez en créer une vous-même si vous le souhaitez; dans ce cas, vous devriez probablement le placer sous/usr/local, ajoutez-le à/etc/shells et chsh. Ensuite, déconnectez-vous et reconnectez-vous, en vous faisant peut-être une idée à vous @ localhost ou en vous soumettant d'abord comme test.

10
dstromberg

J'ai écrit une méthode pour consigner toutes les commandes/commandes intégrées dans un fichier texte ou un serveur 'syslog' sans utiliser de correctif ni d'exécutable spécial.

Il est très facile à déployer, car il s’agit d’un simple script shell qui doit être appelé une fois à l’initialisation du 'bash'.

Voir la méthode ici .

3

Merci pour les conseils! J'ai fini par utiliser ceci:

#created by francois scheurer

#sourced by '~/.bashrc', which is the last runned startup script for bash invocation
#for login interactive, login non-interactive and non-login interactive shells.
#note that a user can easily avoid calling this file by using options like '--norc';
#he also can unset or overwrite variables like 'Prompt_COMMAND'.
#therefore it is useful for audit but not for security.

#Prompt & color
#http://www.pixelbeat.org/docs/terminal_colours/#256
#http://www.frexx.de/xterm-256-notes/
_backnone="\e[00m"
_backblack="\e[40m"
_backblue="\e[44m"
_frontred_b="\e[01;31m"
_frontgreen_b="\e[01;32m"
_frontgrey_b="\e[01;37m"
_frontgrey="\e[00;37m"
_frontblue_b="\e[01;34m"
PS1="\[${_backblue}${_frontgreen_b}\]\u@\h:\[${_backblack}${_frontblue_b}\]\w\\$\[${_backnone}${_frontgreen_b}\] "

#'history' options
declare -rx HISTFILE="$HOME/.bash_history"
chattr +a "$HISTFILE" # set append-only
declare -rx HISTSIZE=500000 #nbr of cmds in memory
declare -rx HISTFILESIZE=500000 #nbr of cmds on file
declare -rx HISTCONTROL="" #does not ignore spaces or duplicates
declare -rx HISTIGNORE="" #does not ignore patterns
declare -rx HISTCMD #history line number
history -r #to reload history from file if a prior HISTSIZE has truncated it
if groups | grep -q root; then declare -x TMOUT=3600; fi #timeout for root's sessions

#enable forward search (ctrl-s)
#http://ruslanspivak.com/2010/11/25/bash-history-incremental-search-forward/
stty -ixon

#history substitution ask for a confirmation
shopt -s histverify

#add timestamps in history - obsoleted with logger/syslog
#http://www.thegeekstuff.com/2008/08/15-examples-to-master-linux-command-line-history/#more-130
#declare -rx HISTTIMEFORMAT='%F %T '

#bash audit & traceabilty
#
#
declare -rx AUDIT_LOGINUSER="$(who -mu | awk '{print $1}')"
declare -rx AUDIT_LOGINPID="$(who -mu | awk '{print $6}')"
declare -rx AUDIT_USER="$USER" #defined by pam during su/Sudo
declare -rx AUDIT_PID="$$"
declare -rx AUDIT_TTY="$(who -mu | awk '{print $2}')"
declare -rx AUDIT_SSH="$([ -n "$SSH_CONNECTION" ] && echo "$SSH_CONNECTION" | awk '{print $1":"$2"->"$3":"$4}')"
declare -rx AUDIT_STR="[audit $AUDIT_LOGINUSER/$AUDIT_LOGINPID as $AUDIT_USER/$AUDIT_PID on $AUDIT_TTY/$AUDIT_SSH]"
declare -rx AUDIT_SYSLOG="1" #to use a local syslogd
#
#Prompt_COMMAND solution is working but the syslog message are sent *after* the command execution, 
#this causes 'su' or 'Sudo' commands to appear only after logouts, and 'cd' commands to display wrong working directory
#http://jablonskis.org/2011/howto-log-bash-history-to-syslog/
#declare -rx Prompt_COMMAND='history -a >(tee -a ~/.bash_history | logger -p user.info -t "$AUDIT_STR $PWD")' #avoid subshells here or duplicate execution will occurs!
#
#another solution is to use 'trap' DEBUG, which is executed *before* the command.
#http://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command
#http://www.davidpashley.com/articles/xterm-titles-with-bash.html
#set -o functrace; trap 'echo -ne "===$BASH_COMMAND===${_backvoid}${_frontgrey}\n"' DEBUG
set +o functrace #disable trap DEBUG inherited in functions, command substitutions or subshells, normally the default setting already
#enable extended pattern matching operators
shopt -s extglob
#function audit_DEBUG() {
#  echo -ne "${_backnone}${_frontgrey}"
#  (history -a >(logger -p user.info -t "$AUDIT_STR $PWD" < <(tee -a ~/.bash_history))) && sync && history -c && history -r
#  #http://stackoverflow.com/questions/103944/real-time-history-export-amongst-bash-terminal-windows
#  #'history -c && history -r' force a refresh of the history because 'history -a' was called within a subshell and therefore
#  #the new history commands that are appent to file will keep their "new" status outside of the subshell, causing their logging
#  #to re-occur on every function call...
#  #note that without the subshell, piped bash commands would hang... (it seems that the trap + process substitution interfer with stdin redirection)
#  #and with the subshell
#}
##enable trap DEBUG inherited for all subsequent functions; required to audit commands beginning with the char '(' for a subshell
#set -o functrace #=> problem: completion in commands avoid logging them
function audit_DEBUG() {
    #simplier and quicker version! avoid 'sync' and 'history -r' that are time consuming!
    if [ "$BASH_COMMAND" != "$Prompt_COMMAND" ] #avoid logging unexecuted commands after Ctrl-C or Empty+Enter
    then
        echo -ne "${_backnone}${_frontgrey}"
        local AUDIT_CMD="$(history 1)" #current history command
        #remove in last history cmd its line number (if any) and send to syslog
        if [ -n "$AUDIT_SYSLOG" ]
        then
            if ! logger -p user.info -t "$AUDIT_STR $PWD" "${AUDIT_CMD##*( )?(+([0-9])[^0-9])*( )}"
            then
                echo error "$AUDIT_STR $PWD" "${AUDIT_CMD##*( )?(+([0-9])[^0-9])*( )}"
            fi
        else
            echo $( date +%F_%H:%M:%S ) "$AUDIT_STR $PWD" "${AUDIT_CMD##*( )?(+([0-9])[^0-9])*( )}" >>/var/log/userlog.info
        fi
    fi
    #echo "===cmd:$BASH_COMMAND/subshell:$BASH_SUBSHELL/fc:$(fc -l -1)/history:$(history 1)/histline:${AUDIT_CMD%%+([^ 0-9])*}===" #for debugging
}
function audit_EXIT() {
    local AUDIT_STATUS="$?"
    if [ -n "$AUDIT_SYSLOG" ]
    then
        logger -p user.info -t "$AUDIT_STR" "#=== bash session ended. ==="
    else
        echo $( date +%F_%H:%M:%S ) "$AUDIT_STR" "#=== bash session ended. ===" >>/var/log/userlog.info
    fi
    exit "$AUDIT_STATUS"
}
#make audit trap functions readonly; disable trap DEBUG inherited (normally the default setting already)
declare -fr +t audit_DEBUG
declare -fr +t audit_EXIT
if [ -n "$AUDIT_SYSLOG" ]
then
    logger -p user.info -t "$AUDIT_STR" "#=== New bash session started. ===" #audit the session openning
else
    echo $( date +%F_%H:%M:%S ) "$AUDIT_STR" "#=== New bash session started. ===" >>/var/log/userlog.info
fi
#when a bash command is executed it launches first the audit_DEBUG(),
#then the trap DEBUG is disabled to avoid a useless rerun of audit_DEBUG() during the execution of pipes-commands;
#at the end, when the Prompt is displayed, re-enable the trap DEBUG
declare -rx Prompt_COMMAND="trap 'audit_DEBUG; trap DEBUG' DEBUG"
declare -rx BASH_COMMAND #current command executed by user or a trap
declare -rx SHELLOPT #Shell options, like functrace  
trap audit_EXIT EXIT #audit the session closing

Prendre plaisir!

3
francois scheurer