ユーザーから変数へのいくつかの入力を取得するためにダイアログボックスを使用しています。問題は、現在のスクリプトでユーザーが問題を引き起こすすべての値を入力できることです。そのため、入力に英数字と下線のみを含むルールを作成したいと思います。
#!/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