3

The script below creates a dialog box. The user can press Enter at any time, and then the script continues. I need to block the Enter key to ensure that the user fills all the fields. How can I do that?

#!/bin/bash
shell=""
groups=""
user=""
home=""
exec 3>&1
dialog --separate-widget $'\n' --ok-label "Ok" \
          --backtitle "Linux User Managment" \
          --title "new" \
          --form "creating new " \
15 80 0 \
        "field1: "              1 1 "$user"             1 25 40 0 \
        "field2:"               2 1 "$shell"            2 25 40 0 \
        "field3:"               3 1 "$groups"           3 25 40 0 \
        "field4:"               4 1 "$home"             4 25 40 0 \

# aggiungi dimensione quota

2>&1 1>&3 | {
  read -r user
  read -r shell
  read -r groups
  read -r home

  echo $user
  echo $shell
  echo $groups
  echo $home

  #continue script here
}
exec 3>&-

enter image description here

1 Answer 1

2

I don't think that the Dialog program has an option to validate the content of fields. Have your program run dialog in a loop, throwing the user back into the dialog box if they enter incorrect values such as empty fields.

#!/bin/bash
shell=
groups=
user=
home=
error_message=
IFS=$'\n'; set -f
while [[ -z $shell || -z $groups || -z $user || -z $home ]]; do
  set $(dialog … --form="creating new $error_message" … --output-fd=3 3>&1 >/dev/tty)
  shell=$1 groups=$1 user=$1 home=$1
  error_message="(fields must not be empty)"
done
unset IFS; set +f

You must log in to answer this question.

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