Skip to content

Instantly share code, notes, and snippets.

@mtfurlan
Last active July 27, 2020 01:31
Show Gist options
  • Save mtfurlan/040147edac1fe3e9cf0958d45a694afa to your computer and use it in GitHub Desktop.
Save mtfurlan/040147edac1fe3e9cf0958d45a694afa to your computer and use it in GitHub Desktop.
Bash read line loop
#!/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