Created
January 27, 2016 23:08
-
-
Save wajatimur/9968043f5274362c9f4d to your computer and use it in GitHub Desktop.
Search command line history and run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
IFS=$'\n' | |
HISTFILE=~/.zsh_history | |
set -o history | |
declare -a H | |
HISTORY_CMD="" | |
function displayList { | |
IDX=0 | |
for cmd in ${H[@]}; do | |
let IDX+=1 | |
if [ -n "${H[$IDX]}" ]; then | |
echo "[$IDX] ${H[$IDX]}" | |
fi | |
done | |
} | |
function execCommand { | |
if [ -n "$1" ]; | |
then | |
HISTORY_CMD=${H[$1]} | |
fi | |
} | |
H=( $(history | grep -oE '(?)\;(.*)$' | cut -c2- | grep $1 | grep -ovE '^history(.*)' | grep -ovE '^(./historun|historun)(.*)') ) | |
if [ ${#H[@]} -gt 0 ]; | |
then | |
displayList | |
else | |
echo "Sorry no matches found!" | |
exit | |
fi | |
read -p "Please enter your history ID, enter 'q' to cancel [q]: " HISTORY_ID | |
if [ $HISTORY_ID == 'q' ]; | |
then | |
exit | |
else | |
execCommand $HISTORY_ID | |
fi | |
if [ -n "$HISTORY_CMD" ]; | |
then | |
eval $HISTORY_CMD | |
else | |
displayList | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment