入力待ち処理(read)
処理の途中で入力を待ち、入力された文字を変数に格納する際に使用します。
- 構文
read -p "[メッセージ]" [変数名]
- サンプル
ユーザ名とパスワードの入力を待ち、確認を行う例
入力後、チェック結果が[n]である場合、処理を終了する。
read -p "Enter a username : " USER read -p "Enter the user password : " PASSWORD cat <<EOF -------------------------- Set the following information. USERNAME : $USER PASSWORD : $PASSWORD -------------------------- EOF read -p "Are you sure you want to use these settings? (Y/n): " SETUSER case "$SETUSER" in [yY]) echo "continue" ;; *) echo "Ends the process." ; exit 0 ;; esac
Last-modified: 2022-02-06 (日) 17:44:54 (802d)