1 votes

Restaurer un historique spécifique du terminal à partir de .bash_sessions ?

Mon terminal Windows ont été restaurés correctement après la mise à jour vers High Sierra - mais chacun d'entre eux ne contient qu'un seul onglet ; aucun des autres onglets n'a été restauré.

J'ai tous les fichiers d'historique de ~/.bash_sessions, donc je pense que dans le pire des cas, je ne pourrai récupérer l'historique des commandes qu'en ouvrant manuellement de nouveaux onglets et en remplaçant les historiques.

Quelqu'un connaît-il un moyen de récupérer l'historique de défilement, compte tenu de ces ID de session (ou peut-être des GUID de fenêtre, à partir de l'ID de session) ? com.apple.Terminal.savedState/windows.plist ) ?

1 votes

En théorie, si vous pouvez faire en sorte que $BASH_SESSION ou $TERM_SESSION_ID soit défini sur l'identifiant approprié au démarrage de l'interpréteur de commandes, il importera l'historique correct depuis .bash_sessions : reddit.com/r/osx/comments/397uep/ . Je ne sais pas si/comment vous pouvez faire cela, mais cela pourrait vous donner, à vous ou à quelqu'un d'autre, quelques idées...

1voto

zdub Points 121

Après quelques recherches sur Google, j'ai trouvé este script sur pastebin d'un utilisateur anonyme. Je n'ai pas eu l'occasion de l'essayer moi-même, mais il semble qu'il ait du potentiel avec quelques modifications.

# Separate Command Histories for Restored Terminal Sessions

# Terminal assigns each terminal session a unique identifier and
# communicates it via the TERM_SESSION_ID environment variable so that
# programs running in a terminal can save/restore application-specific
# state when quitting and restarting Terminal with Resume enabled.

# The following script saves and restores the bash command history
# independently for each restored terminal session. It also merges
# commands into the global history for new sessions. Because of this
# it is recommended that you set HISTSIZE and HISTFILESIZE to larger
# values (their default value is 500). Old sessions are periodically
# deleted.

if [ -z "$BASH_SESSION" ] && [ -n "$TERM_SESSION_ID" ]; then

    # Only perform this setup once per shell session (which shouldn't
    # happen unless the user's ~/.bash_profile executes /etc/profile,
    # which is normally redundant).
    BASH_SESSION=1

    # Set up the session directory/file.
    if [ -z "$BASH_SESSION_DIR" ]; then
        BASH_SESSION_DIR="$HOME/.bash_sessions"
        BASH_SESSION_FILE="$BASH_SESSION_DIR/$TERM_SESSION_ID.session"
    elif [ -z "$BASH_SESSION_FILE" ]; then
        BASH_SESSION_FILE="$BASH_SESSION_DIR/$TERM_SESSION_ID.session"
    fi
    mkdir -p "$BASH_SESSION_DIR"

    # Arrange for session-specific shell command history. Users can
    # disable this via the existence of the following file.
    [ -f ~/.bash_session_no_history ] && BASH_SESSION_HISTORY=0 || BASH_SESSION_HISTORY=1
    if [ $BASH_SESSION_HISTORY -eq 1 ]; then
        BASH_SHARED_HISTFILE="$HISTFILE"
        BASH_SESSION_HISTFILE="$BASH_SESSION_DIR/$TERM_SESSION_ID.history"
        BASH_SESSION_HISTFILE_NEW="$BASH_SESSION_DIR/$TERM_SESSION_ID.historynew"
        # If the session history doesn't exist, copy the shared history
        if [ -f "$BASH_SHARED_HISTFILE" ] && [ ! -f "$BASH_SESSION_HISTFILE" ]; then
            cp "$BASH_SHARED_HISTFILE" "$BASH_SESSION_HISTFILE"
        else
            # Ensure the file exists and doesn't get expired.
            touch "$BASH_SESSION_HISTFILE"
        fi
        history -r "$BASH_SESSION_HISTFILE"
        : >| "$BASH_SESSION_HISTFILE_NEW"
        HISTFILE="$BASH_SESSION_HISTFILE_NEW"
    fi

    if [ "$SHLVL" -eq 1 ]; then
        # Restore previous session state.
        if [ -f "$BASH_SESSION_FILE" ]; then
            . "$BASH_SESSION_FILE"
            rm "$BASH_SESSION_FILE"
        fi

        # Save the current state.
        bash_save_session_state() {
            if [ "$SHLVL" -eq 1 ] && [ -n "$BASH_SESSION_FILE" ]; then
                echo -n Saving session...
                echo echo Restored session: $(date) > "$BASH_SESSION_FILE"

                # Users can add custom state by defining the following
                # function. e.g., to save an environment variable:
                #   bash_session_save_state() { echo MY_VAR="'$MY_VAR'" >> "$BASH_SESSION_FILE"; }
                declare -F bash_session_save_state >/dev/null && bash_session_save_state

                # Save new history commands.
                if [ $BASH_SESSION_HISTORY -eq 1 ]; then
                    history -a
                    cat "$BASH_SESSION_HISTFILE_NEW" >> "$BASH_SHARED_HISTFILE"
                    cat "$BASH_SESSION_HISTFILE_NEW" >> "$BASH_SESSION_HISTFILE"
                    # Empty this session's history file to keep track of
                    # which commands have already been copied.
                    : >| "$BASH_SESSION_HISTFILE_NEW"
                    # Read/write the files via the history command so they
                    # are truncated as appropriate.
                    history -r "$BASH_SHARED_HISTFILE"
                    history -w "$BASH_SHARED_HISTFILE"
                    history -r "$BASH_SESSION_HISTFILE"
                    history -w "$BASH_SESSION_HISTFILE"
                fi

                echo completed.
            fi
        }

        # Delete old session files. Do not do this more frequently
        # than once a day.
        BASH_SESSION_TIMESTAMP_FILE="$BASH_SESSION_DIR/_expiration_check_timestamp"
        bash_delete_expired_session_state() {
            if ([ ! -f "$BASH_SESSION_TIMESTAMP_FILE" ] || [ -z $(find "$BASH_SESSION_TIMESTAMP_FILE" -mtime -1d) ]); then
                local bash_session_lock_file="$BASH_SESSION_DIR/_expiration_lockfile"
                if shlock -f "$bash_session_lock_file" -p $$; then
                    echo -n Deleting expired sessions...
                    local delete_count=$(find "$BASH_SESSION_DIR" -type f -mtime +2w -print -delete | wc -l)
                    [ "$delete_count" -gt 0 ] && echo $delete_count completed. || echo none found.
                    touch "$BASH_SESSION_TIMESTAMP_FILE"
                    rm "$bash_session_lock_file"
                fi
            fi
        }

        # Update saved session state when exiting.
        bash_update_session_state() {
            bash_save_session_state && bash_delete_expired_session_state
        }
        trap bash_update_session_state EXIT
    fi
fi

LesApples.com

LesApples est une communauté de Apple où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres utilisateurs d'appareils Apple, poser vos propres questions ou résoudre celles des autres.

Powered by:

X