Enterキーを押さずにBash選択オプション

Enterキーを押さずにBash選択オプション

次のスクリプトがあります。

#!/bin/bash

clear

echo -n "Choose an option:
1) Encrypt
2) Decrypt
3) Quit
-> "

read opzione

if [ "$opzione" == "1" ]; then

echo -n "Drag here the file to encrypt: "
read chiaro
...
fi

数字を入力した後、スクリプトが自動的に数字を受け入れるようにしたいと思います。可能ですか?

ありがとう

ベストアンサー1

次のオプションが利用可能です-n charshelp read

  -n nchars return after reading NCHARS characters rather than waiting
        for a newline, but honor a delimiter if fewer than NCHARS
        characters are read before the delimiter

あるいは、最新バージョンのbashでは、設定全体をselect選択リストを実装する単一のステートメントに置き換えることができます。

$ select opzione in "Encrypt" "Decrypt" "Quit"; do echo "You chose $REPLY"; done
1) Encrypt
2) Decrypt
3) Quit
#? 1
You chose 1
#? ^C

おすすめ記事