DEV Community

lbonanomi
lbonanomi

Posted on

What was I doing in this shell?

Do you frequently open another SSH window for another task or to connect to another machine? I know I do. Do you ever return to those windows a day or two later and wonder what you were doing there to begin-with? I know I do.

Put this into your .profile to to run the lulwat function any time you hit enter three times in a row.

# source me.

export PROMPT_COMMAND=btnMash

function btnMash {
  # Init a counter for prompts-displayed if its missing
  [[ -f /tmp/promptsDisplayed ]] || history | tail -1 | awk '{ print $1 }' > /tmp/promptsDisplayed;

  # Init a counter for commands
  [[ -f /tmp/commandsRun ]] || (history | tail -1 | awk '{ print $1 }' > /tmp/commandsRun)

  COMMANDS=$(cat /tmp/commandsRun)

  # Increment prompts-displayed counter
  echo $(($(cat /tmp/promptsDisplayed)+1 )) > /tmp/promptsDisplayed;

  history | tail -1 | awk '{ print $1 }' > /tmp/commandsRun

  if [[ $COMMANDS -lt $(cat /tmp/commandsRun) ]]
  then
    cat /tmp/commandsRun > /tmp/promptsDisplayed
  fi

  PROMPTS=$(cat /tmp/promptsDisplayed)

  if [[ $PROMPTS -ge $(($COMMANDS+3)) ]]
  then
    lulwat; rm /tmp/promptsDisplayed; return
  fi
}

function lulwat {
    # If I am su-ed into this account, tell me.
    #
    ID=$(id | awk '{gsub(/[\(-\)]/," ")} { print $2 }');
    WHO=$(who -m | awk '{ print $1 }')

    [[ "$ID" == "$WHO" ]] && echo "You are: $ID"
    [[ "$ID" == "$WHO" ]] || echo "You are: $ID (su-ed from $WHO)"

    # Hostname and path
    #
    printf "On: $(hostname):$(pwd)\n"

    # If this a git repo, remind me of the remote
    #
    git status &>/dev/null && (
        printf "$(git remote -vv | awk '{ print $2 }' | head -1) ($(git branch | awk '{ print $2 }'))\n"
    )
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)