Ripristina la cronologia del terminale specifica da .bash_sessions?

0

Il mio terminale windows è stato ripristinato correttamente dopo l'aggiornamento a High Sierra, ma ognuno di essi ha una sola scheda al suo interno; nessuna delle altre schede ripristinate.

Ho tutti i file di cronologia da ~ / .bash_sessions, quindi penso che il caso peggiore sia che sono solo in grado di recuperare la cronologia dei comandi aprendo manualmente nuove schede e sostituendo le cronologie.

Qualcuno sa come ripristinare la cronologia di scorrimento, dati questi ID di sessione (o forse i GUID della finestra, da com.apple.Terminal.savedState/windows.plist )?

    
posta abathur 08.01.2018 - 07:15
fonte

1 risposta

1

Dopo aver fatto qualche ricerca su google, ho trovato questo script su pastebin da un utente anonimo. Non ho avuto la possibilità di provarlo da solo, ma sembra che abbia delle potenzialità con alcune modifiche.

# 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
    
risposta data 11.01.2018 - 01:15
fonte

Leggi altre domande sui tag