Last active
July 27, 2020 01:31
-
-
Save mtfurlan/040147edac1fe3e9cf0958d45a694afa to your computer and use it in GitHub Desktop.
Bash read line loop
This file contains 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 | |
historyDir=/tmp/historyTest | |
function handle() { | |
echo "now run '$1'" | |
} | |
function cleanup() { | |
history -w $historyDir/$UID | |
} | |
function ctrl_c() { | |
echo | |
cleanup | |
exit 1 | |
} | |
trap ctrl_c INT | |
mkdir -p $historyDir | |
history -r $historyDir/$UID | |
set -o vi | |
cmd="" | |
while true; do | |
read -p '> ' -e cmd | |
history -s "$cmd" | |
case "$cmd" in | |
exit) cleanup; exit 0; ;; | |
history) history; ;; | |
*) handle "$cmd"; ;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment