Linux 允许用户仅选择一组显示的ID

Linux 允许用户仅选择一组显示的ID,linux,bash,shell,centos7,Linux,Bash,Shell,Centos7,我真的不知道这叫什么,所以是的,基本上我想让用户只选择显示的ID(见图)。我正在考虑使用一个变量并将这些数字作为数组保存,但我不知道,因为我还在学习bash脚本。谢谢大家! 我的剧本是这样的 read -p "Select accessory category below." mysql -D snipeit -e "SELECT id AS ID, name AS Name FROM categories WHERE category_type='accessory';" read -p "A

我真的不知道这叫什么,所以是的,基本上我想让用户只选择显示的ID(见图)。我正在考虑使用一个变量并将这些数字作为数组保存,但我不知道,因为我还在学习bash脚本。谢谢大家!

我的剧本是这样的

read -p "Select accessory category below."
mysql -D snipeit -e "SELECT id AS ID, name AS Name FROM categories WHERE category_type='accessory';"
read -p "Accessory category: " accessoryCateg
if *(allow only 4, 5, 7, 8, 9, 10, 11, 12, 13, 14 to be selected)*
*inserts data into database*
else echo "Try again!"
fi

您可以尝试使用
选择
命令:

echo "Select accessory category below."
sample1=`mysql -D snipeit -e "SELECT id AS ID FROM categories WHERE category_type='accessory';"`
options=(`echo $sample1 | cut -d ' ' -f2-`)

PS3="Please select a category: "
select accessoryCateg in "${options[@]}"
do
echo "selected $accessoryCateg"
#inserts data into database
break;
done
我用计算机解决了这个问题

echo "Select accessory category below."
mysql -D snipeit -e "SELECT id AS ID, name AS Name FROM categories WHERE category_type='accessory';"
read -p "Accessory category: " userAccCateg
varAccCateg=$(mysql -D snipeit -e "SELECT id FROM categories WHERE category_type='accessory';")
accCateg=(`echo $varAccCateg | cut -d ' ' -f2-`)

if [[ " ${accCateg[@]} " =~ " ${userAccCateg} " ]]; then
    echo "You have chosen $userAccCateg"
else
    echo "Wrong choice!"
fi
结果是这样的


感谢@zhliu03和@sjsam的帮助

谢谢你,我会试试这个,如果有问题我会报告的works@PauloBernardo:谢谢,非常感谢我今天才注意到,我应该把这件事放在哪里?在您的代码中,您已完成2项,但只有1项。您的代码将在下面显示“选择附件类别”。1) 4 5 7 8 9 10 11 12 13 14请选择一个类别:4 selectedoh,带有选项的行=。。。需要更改,我已经编辑了我以前的帖子。
echo "Select accessory category below."
mysql -D snipeit -e "SELECT id AS ID, name AS Name FROM categories WHERE category_type='accessory';"
read -p "Accessory category: " userAccCateg
varAccCateg=$(mysql -D snipeit -e "SELECT id FROM categories WHERE category_type='accessory';")
accCateg=(`echo $varAccCateg | cut -d ' ' -f2-`)

if [[ " ${accCateg[@]} " =~ " ${userAccCateg} " ]]; then
    echo "You have chosen $userAccCateg"
else
    echo "Wrong choice!"
fi