1

Today I have learned some tricks about menu option in command line. One of these was

cat << EOF
    Some lines
EOF
read -n1 -s
case $newvar in
   "1") echo "";
ecsa 

It's really magical. I can't find any description in man page about this option. How the input to read command was pushed into case option ? It usually use a variable to do this thing as I know.

I just want to understand the process of this combination further.

while :
do
    clear
    cat<<EOF
    ==============================
    Menu Install DHCP Tool
    ------------------------------
    Please enter your choice:

    (1) Config Network Interface
    (2) Check status
    (3) Config DHCP server
    (Q)uit
    ------------------------------
EOF
read -n1 -s
    case "$REPLY" in
    "1")  config_network ;;
    "2")  check_status ;;
    "3")  config_dhcp ;;
    "q")  exit                      ;;
     * )  echo "invalid option"     ;;
    esac
    sleep 0.2
done
4
  • 1
    Post your actual code. Apr 5, 2017 at 3:40
  • Added as you wish. Apr 5, 2017 at 4:03
  • IMHO, this is a pretty convoluted way to create a menu, bash has an inbuilt menu system for doing what you have described, and allows you to use less code, which means its more readable. you can find documentation on it here man bash | less '+/^\s*select' Apr 5, 2017 at 4:28
  • @the_velour_fog, Seems rather straightforward to me. And doing it manually allows control over how it looks and what the hotkeys are.
    – ilkkachu
    Apr 5, 2017 at 8:23

2 Answers 2

4

The documentation of read notes that:

If no names are supplied, the line read is assigned to the variable REPLY.

From that point it's a normal case statement. -n1 reads a single byte and -s turns off terminal echo of the input.

2
  • And how about option -n1 -s , what does it mean ? Apr 5, 2017 at 4:42
  • -n1 reads a single byte and -s turns off terminal echo. They are unrelated. Apr 5, 2017 at 4:44
0

The variable $newvar you use in your short description doesn't exist.

It must be $REPLY, as you actually use in the full command.

This issues have no relation to the read options -n1 -s you are asking about.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .