对话框 - 用户输入必须是字母数字和下划线

对话框 - 用户输入必须是字母数字和下划线

我正在使用对话框从用户处获取一些输入到变量,我的问题是在我当前的脚本中用户可以输入任何导致问题的值。所以我想为输入制定规则,使其仅包含字母数字和下划线。

#!/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

相关内容