switch statement - Bash - read case with multiple selections -
i'm using following code in script.
#!/bin/bash while true; read -p "do choice: [1] [2] [3] [4] [e]xit: " choice case "$choice" in [1]* ) echo -e "$choice\n"; break;; [2]* ) echo -e "$choice\n"; break;; [3]* ) echo -e "$choice\n"; break;; [4]* ) echo -e "$choice\n"; break;; [ee]* ) echo "exited user"; exit;; * ) echo "are kidding me???";; esac done my question is, how can script accept multiple choices. input like: 1,4, run case [1] , [4]?
set ifs include commas:
ifs=', ' then process choices in loop (note -a flag read input treated array):
while true; read -p "do choice: [1] [2] [3] [4] [e]xit: " -a array choice in "${array[@]}"; case "$choice" in [1]* ) echo -e "$choice\n";; [2]* ) echo -e "$choice\n";; [3]* ) echo -e "$choice\n";; [4]* ) echo -e "$choice\n";; [ee]* ) echo "exited user"; exit;; * ) echo "are kidding me???";; esac done done
Comments
Post a Comment