下面的脚本创建一个对话框。用户可以Enter随时按下,然后脚本继续。我需要阻止该Enter键以确保用户填写所有字段。我该怎么做?
#!/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>&-
答案1
我不认为对话框程序有验证字段内容的选项。让您的程序dialog
循环运行,如果用户输入不正确的值(例如空字段),则将用户带回对话框。
#!/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