I need to prompt the user to enter a name of a class that comes from CSV file:
Computer,Course 1,This course is taught by Dr. Chen
Information technology,Course 2,This course is taught by Dr. Weiss
Database,Course 3,This course is taught by Dr. Gonzalas
Algorithm,Course 4,This course is taught by Dr. Sanabria
Computer,Course 5,This course is taught by Dr. Sun
Data mining,Course 6,This course is taught by Dr. Li
Algorithm,Course 8,This course is taught by Dr. Xue
The user will enter one of the items in Column 1. As you can see, there are duplicates. There needs to be a way to prompt the user to clarify which class he is going to be requesting information about, and then continue with the selection.
How can I do that?
Please take into consideration that this is just a draft, which runs on my system and has not been tested on other
#!/bin/bash
IFS=$"\n"
# Read user input
read -p "Type a name of class: " class
# Results in two views - entries count and entries content
entries_cnt=`cat ./tst.csv | cut -d',' -f1 | grep -cw "$class"`
entries_nfo=$(cat tst.csv | awk -v class=$class -F"," '{if ($1 ~ class) print $0 }')
# If one entry founded
if [ "$entries_cnt" == "1" ] ; then
echo 'Entry founded:'
echo $entries_nfo
# If entry not founded
elif [ "$entries_cnt" \< "1" ] ; then
echo 'Entry not found'
# If founded more than 1 entry
else
echo 'Founded more than 1 entry:'
echo $entries_nfo;
echo 'Available courses:'
echo $entries_nfo | cut -d',' -f2
# Read user input
read -p 'Input course: ' course
echo $entries_nfo | awk -v course=$course -F"," '{if ($2 ~ course) print $0 }'
fi