사용자로부터 변수에 대한 일부 입력을 얻기 위해 대화 상자를 사용하고 있습니다. 문제는 현재 스크립트에서 사용자가 문제를 일으키는 모든 값을 입력할 수 있다는 것입니다. 그래서 입력에 영숫자와 밑줄만 포함하는 규칙을 만들고 싶습니다.
#!/bin/sh
# The Dialog Menu
while true; do
menu="$(dialog --title "bhyve" --backtitle "bhyve, the BSD hypervisor" \
--stdout --nocancel --menu "Select your OS:" 0 0 0 \
1 "FreeBSD 12.2" 2 "Exit")"
if [ "${menu}" -eq 1 ]; then
while true; do
if dialog --title "FreeBSD" --stdout --nocancel --inputbox "The VM name:" 0 0 FreeBSD \
--and-widget --title "FreeBSD" --nocancel --inputbox "RAM (GB):" 0 0 8 \
--and-widget --title "FreeBSD" --nocancel --inputbox "CPUs:" 0 0 2 \
--and-widget --title "FreeBSD" --nocancel --inputbox "Disk size (GB):" 0 0 32 > output.txt; then
name="$(< output.txt awk '{print $1}')"
ram="$(< output.txt awk '{print $2}')"
cpu="$(< output.txt awk '{print $3}')"
disk="$(< output.txt awk '{print $4}')"
vmd="/zroot/VMs"
freebsd_iso="/zroot/VMs/ISOs/FreeBSD-12.2-RELEASE-amd64-disc1.iso"
if [ ! -d "${vmd}" ]; then
dialog --title "bhyve" --backtitle "bhyve, the BSD hypervisor" \
--msgbox "/zroot/VMs directory does not exist, please run Setup first" 5 65
rm output.txt
exit
elif [ ! -f "${freebsd_iso}" ]; then
#pkg install ca_root_nss
fetch -o /zroot/VMs/ISOs https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/12.2/FreeBSD-12.2-RELEASE-amd64-disc1.iso
elif [ -f "/usr/local/etc/rc.d/${name}" ]; then
dialog --title "bhyve" --backtitle "bhyve, the BSD hypervisor" \
--msgbox "The VM ${name} already exists, please choose another name" 5 65
break;
fi
freebsd > /dev/null 2>&1
dialog --title "bhyve" --backtitle "bhyve, the BSD hypervisor" \
--msgbox "The VM ${name} has been created" 0 0
rm output.txt
else
rm output.txt
break;
fi
done
elif [ "${menu}" -eq 2 ]; then
dialog --title "bhyve" --backtitle "bhyve, the BSD hypervisor" \
--no-label "Yes" --yes-label "No" \
--yesno "Would you like to exit?" 0 0
if [ $? -eq 1 ]; then
break;
fi
fi
done