Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

shell - How to select option when click “Enter” button?

I have this code, im clicking "y" button to Skip, i want change it to Enter button.

Codes:

echo -e "(corona) > Press "Y" to Exit... c"
read handler
if [ "$handler" == "y" ]; then
echo -e "(corona) > Exiting... c"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Usually, the Enter button will result in a newline, causing the response to be empty string.

echo -e "(corona) > Press "Enter" to Exit... c"
read handler
if [ "$handler" == "" ]; then
    echo -e "(corona) > Exiting... c"
    ...
fi

You can also combine the echo/read to use the prompt option of read

read -p "(corona) > Press "Enter" to Exit..." handler
if [ "$handler" == "" ]; then
    echo -e "(corona) > Exiting... c"
    ...
fi


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...