Shell script - echo a question, read response? half type the answer?

Shell script - echo a question, read response? half type the answer?

I am wanting to ask the user what date they would like to enter, this reads a response and then navigates to a directory based on what they put.

The majority of the time, it's going to be today's date. I wonder, can I do

echo "What date would you like?"
read date

and then output the current date to their command line, so they can either press enter - or they can backspace and change to last month, for example?

答案1

With the zsh shell:

zmodload zsh/datetime
strftime -s date %F $EPOCHSECONDS
set -o emacs
vared -p 'What date would you like? ' date

(

or using prompt expansion with print -P:

print -rPv date '%D{%F}'
set -o emacs
vared -p 'What date would you like? ' date

or (an other (and older) way to get prompt expansion):

date=${(%):-%D{%F}}
set -o emacs
vared -p 'What date would you like? ' date

)

vared is the builtin command to edit a variable.

With the fish shell:

fish_default_key_bindings
read -c (date +%F) -P 'What date would you like? ' date

With the ksh93 shell:

date=${ printf '%(%F)T' now; }
set -o emacs
IFS= read -rv 'date?What date would you like? '

With the bash shell:

printf -v date '%(%F)T' -1
set -o emacs
IFS= read -rei "$date" -p 'What date would you like? ' date

All would pre-seed the line editor with the current date and let you edit it (with emacs-style key binding; replace emacs/default with vi if you prefer vi style).

Here %F (short for %Y-%m-%d) is standard strftime() directive to give you the date in international YYYY-MM-DD format. You can adapt to your need. See the strftime(3) man page for details.

Note that only zsh and fish approaches work properly if you want to edit a multiline-variable.

答案2

How about this:

read -p "What date would you like (Press enter for today)? " user_date
date="${user_date:-$(date +%Y%m%d)}"

echo "Chosen date is: $date"

The actual date format (%Y%m%d) is up to you.

答案3

You don't state what shell you're using. If you're using Bash, read has this built in.

read -ei "$( date +%F )" -p 'What date would you like? ' USER_DATE
echo "You chose ${USER_DATE}"

Explanation:

-e  Use readline for input.
-i  Specify how to pre-fill the buffer.
-p  Provide a prompt to the user.

Of course, you'd have to include error-checking to ensure that the user entered a valid date, although you can use date itself because it is enormously flexible. Take care with your locale, though, because '03-01-2021' means something different in America (1st March 2021) than it does to the rest of the world (3rd January 2021). Using the format YYYY-MM-DD is likely to work everywhere in the world.

An alternative, if you prefer the GUI, you could use something like Zenity or Yad, which might come built into your system, or available in your standard repositories (they're also available as snaps).

To use Zenity, you might do something like the following (this shows Bash syntax):

function findUserDate
{
    declare -gr USER_DATE="$(
        zenity --entry --title='Date query' --text='What date would you like?' --entry-text="$( date +%F )"
                            )"
    [[ -z ${USER_DATE} ]] && exit 2
    echo "You chose ${USER_DATE}"
}

相关内容