bash一次选择多个答案

bash一次选择多个答案,bash,select,Bash,Select,我有一个名为items的平面文件,我想填充select,但我希望能够一次选择多个项目 项目文件的内容: cat 1 dog 1 pig 1 cherry 2 apple 2 基本脚本: #!/bin/bash PS3=$'\n\nSelect the animals you like: ' options=$(grep '1' items|grep -v '^#' |awk '{ print $1 }') select choice in $options do echo "you se

我有一个名为items的平面文件,我想填充select,但我希望能够一次选择多个项目

项目文件的内容:

cat 1
dog 1
pig 1
cherry 2
apple 2
基本脚本:

#!/bin/bash
PS3=$'\n\nSelect the animals you like: '
options=$(grep '1' items|grep -v '^#' |awk '{ print $1 }')

select choice in $options
do
  echo "you selected: $choice"
done

exit 0
现在的流程是,我一次只能选择一个选项。我希望能够回答1,3或13,并让它回答“您选择:猫猪”

谢谢,


Tazmarine

您不能这样做,但您始终可以记录每个单独的选择:

#!/bin/bash
PS3=$'\n\nSelect the animals you like: '
options=$(grep '1' items|grep -v '^#' |awk '{ print $1 }')

# Array for storing the user's choices
choices=()

select choice in $options Finished
do
  # Stop choosing on this option
  [[ $choice = Finished ]] && break
  # Append the choice to the array
  choices+=( "$choice" )
  echo "$choice, got it. Any others?"
done

# Write out each choice
printf "You selected the following: "
for choice in "${choices[@]}"
do
  printf "%s " "$choice"
done
printf '\n'

exit 0
下面是一个交互示例:

$ ./myscript
1) cat
2) dog
3) pig
4) Finished

Select the animals you like: 3
pig, got it. Any others?

Select the animals you like: 1
cat, got it. Any others?

Select the animals you like: 4
You selected the following: pig cat

如果您希望能够在同一行上编写
3 1
,则必须使用
echo
read
创建自己的菜单循环,这就是我的想法。这似乎是我想要的。我希望最终输出以逗号分隔:

#!/bin/bash

newarray=(all $(grep '1' items|grep -v '^#' |awk '{ print $1 }'))

options() {
num=0
for i in ${newarray[@]}; do
  echo "$num) $i"
  ((num++))
done
}

getanimal() {
while [[ "$show_clean" =~ [A-Za-z] || -z "$show_clean"  ]]; do
  echo "What animal do you like: "
  options
  read -p 'Enter number: ' show
  echo
  show_clean=$(echo $show|sed 's/[,.:;]/ /g')
  selected=$(\
  for s in $show_clean; do
    echo -n "\${newarray[${s}]},"
  done)
  selected_clean=$(echo $selected|sed 's/,$//')
done
eval echo "You selected $selected_clean"
}

getanimal

exit 0

我可以提供一种稍微不同的方法,使用不同的选择提示样式。这里有一个bash函数,允许用户使用箭头键和空格选择多个选项,并用Enter确认。它有一个很好的菜单一样的感觉。我是在他的帮助下写的。可以这样称呼:

多选结果“选项1;选项2;选项3”“真;真”
结果作为数组存储在变量中,其名称作为第一个参数提供。最后一个参数是可选的,用于在默认情况下选择某些选项。看起来是这样的:

函数提示\u用于\u multiselect{
#终端打印控制和按键输入的小助手
ESC=$(printf“\033”)
光标闪烁({printf“$ESC[?25h”;}
光标闪烁关闭(){printf“$ESC[?25l”}
光标_to(){printf“$ESC[$1;${2:-1}H”}
print_inactive(){printf“$2$1”;}
print_active(){printf“$2$ESC[700万美元1$ESC[2700万”}
get_cursor_row(){IFS=';'read-sdR-p$'\E[6n'row COL;echo${row#*[};}
按键输入(){
本地密钥
IFS=read-rsn1键2>/dev/null>&2
如果[[$key=”“];则echo enter;fi;
如果[[$key=$'\x20']];则回显空间;fi;
如果[[$key=$'\x1b']];则
读-rsn2键
如果[[$key=[A]];则回显;fi;
如果[[$key=[B]];则向下回显;fi;
fi
}
切换_选项(){
本地arr_name=$1
eval“local arr=(\“\${${arr\u name}[@]}\”)
本地选项=$2
如果[${arr[option]}==true]];则
arr[选项]=
其他的
arr[选项]=真
fi
eval$arr_name='(“${arr[@]}”)'
}
本地检索值=$1
本地选项
本地默认值

IFS=“;”read-r-a选项我想说,用
选择
是不可能做到的。谢谢你的建议。我确实希望能够使用1 3 4 7或1,3,4,7甚至1:3:4:7。有些列表可能有80多个项目,因此不建议一次选择一个。我必须考虑这一点并发布一些选项。因为项目列表可能会有所不同我想我首先需要为数字定义变量,然后找到一种解析方法。我将在周二更新这篇文章,介绍我的想法。谢谢。非常感谢,这对我帮助很大:)