简体   繁体   中英

If I create a menu in Bash is command style history possible

I have no problem creating the menu - that is not what this question is about.

What happens however is I go from having the arrow keys be useful (scroll up and down to get access to previous commands I've run at the command line) to completely useless ( ^[[A^[[A^[[A^[[B^[[C^[[D^[[C )

Is there any way to encapsulate that behaviour into a menu?

Eg can I use the scroll up and down keys to access previous options I've selected. (It's a BIG menu and I have MANY options like dev.client.alpha or dev.otherclient.beta etc...)

I supposed I could break each one into separate files and just use the command line diredtly OR I could pass an augment to the menu so as to call: ~/menu dev.clint.alpha directly from the command line.

Just curious is anyone else has (had) this itch and if anything has ever been done about it?

Menu I'm presently using is done basically as follows:

while :
   clear
   do
    echo "$MENU"
    read CHOICE ARG1 ARG2 ARG3 ARG4 overflow
    case $CHOICE in
        command.a)
            # do stuff here
            ;;
        command.b)
            # do different stuff here
            ;;
        *) # catch all...
            continue 
            ;;
        esac
    done
clear

You can do what you want by enabling readline on your read, and appending each choice reply to the internal history. You can even save the history to a file so when you rerun the script you have the old history. For example:

HISTFILE=/tmp/myhistory
history -r # read old history
while :
do  echo "MENU. a b q"
    read -e # sets REPLY, enables readline
    history -s "$REPLY" # add to history
    history -w # save to file
    set -- $REPLY
    CHOICE=$1; shift
    ARG1=$1 ARG2=$2 ARG3=$3 ARG4=$4; shift 4
    overflow="$*"
    case $CHOICE in
        a) echo do stuff here $ARG1 ;;
        b) echo do different stuff here ;;
        q) exit ;;
        *) echo catch all...
            continue ;;
    esac
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM