2017-01-31 1 views
-1

を入力するためにユーザーに依頼して、特定のファイルと出力結果をスキャン:私はCSVファイルから来たクラスの名前を入力するようユーザーに促すために必要な(重複して)

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 

ユーザーが入力されます列1のアイテムの1つです。ご覧のとおり、重複があります。どのクラスに情報を要求しているのかを明確にしてから、選択を続けるようにユーザーに促す方法が必要です。

どうすればいいですか?

+0

正確に何が質問ですか? – user1934428

+0

これまでのコードは書いてありますか? StackOverflowは無料のコード作成サービスではありません。 – codeforester

+0

私はbashの 'read'コマンドを見てみることをお勧めします:' help read' – Cyrus

答えて

0

これは私のシステム上で動作するだけでドラフト、と私はコンバインのいくつかと、それの多くを考え出すことができた他の

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

上でテストされていないことを考慮に入れてください。あなたのすべてを助けてください。これは私ができることでした:

#!/bin/sh 
#Start your code from here 


# Clear page and enter variables: date, files, etc. 
clear 
template=Template.email 
d=$(date +%m-%d-%Y) 

grep '[^[:blank:]]' <Dictionary.data> temp.csv 
awk 'NR > 2 {print}' <temp.csv> temp2.csv 

# Welcome message and display catalog/dictionary 
echo "Hello and welcome to the Spring Term of 2017" 
echo "Below are the classes available to you to enroll in." 
echo "Select a course and we will email you further information" 
echo "about the class." 
echo "======================================================" 

# Read file 
while IFS=, read col1 col2 col3 
do 
    echo "$col1" 
done < temp2.csv 

# Ask for student email and class selection 
echo "" 
echo "Please enter your email: " 
read email 

echo "Please pick a class: " 
read course 

    # Open case switch 
    case $course in 

     # Computer case 
     "Computer") 

      # Advise user about duplicates 
      echo "There are two Computer courses with the same name. Please pick the one you like: " 
      echo "1) Course 1 taught by Dr. Chen" 
      echo "2) Course 5 taught by Dr. Sun" 
      read detail 

      # Ask user to pick which class he/she wants 
      case $detail in 

       # Display message and email user desciptions 
       1) 
        echo "We have emailed you the course description. Thank you!" 

        sed -e "s/_CourseNumber_/Course 1/g" -e "s/__Description__/This course is taught by Dr. Chen/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email 
        ;; 

       2) 
        echo "We have emailed you the course description. Thank you!" 

        sed -e "s/_CourseNumber_/Course 5/g" -e "s/__Description__/This course is taught by Dr. Sun/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email 
        ;; 

       *) 

        # Incorrect input 
        echo "Sorry that was not a correct choice." 
        break;; 
      esac 

      ;; 

     # IT case 
     "Information technology") 

      # Display message and email user desciptions 
      echo "We have emailed you the course description. Thank you!" 

      sed -e "s/_CourseNumber_/Course 2/g" -e "s/__Description__/This course is taught by Dr. Weiss/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email 
      ;; 

     # Database case 
     "Database") 

      # Display message and email user desciptions 
      echo "We have emailed you the course description. Thank you!" 

      sed -e "s/_CourseNumber_/Course 2/g" -e "s/__Description__/This course is taught by Dr. Weiss/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email 
      ;; 

     #Algorithm case 
     "Algorithm") 

      # Display message and email user desciptions 
      echo "There are two Algorithm courses with the same name. Please pick the one you like: " 
      echo "1) This course is taught by Dr. Sanabria" 
      echo "2) This course is taught by Dr. Xue" 
      read detail 

      # Ask user to pick which class he/she wants 
      case $detail in 

       # Display message and email user desciptions 
       1) 
        echo "We have emailed you the course description. Thank you!" 

        sed -e "s/_CourseNumber_/Course 4/g" -e "s/__Description__/This course is taught by Dr. Sanabria/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email 
        ;; 

       2) 
        echo "We have emailed you the course description. Thank you!" 

        sed -e "s/_CourseNumber_/Course 8/g" -e "s/__Description__/This course is taught by Dr. Xue/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email 
        ;; 

       *) 

        # Incorrect input 
        echo "Sorry that was not a correct choice." 
        break;; 
      esac 

      ;; 

     # Data Mining case 
     "Data mining") 

      # Display message and email user desciptions 
      echo "We have emailed you the course description. Thank you!" 

      sed -e "s/_CourseNumber_/Course 6/g" -e "s/__Description__/This course is taught by Dr. Li/g" -e "s/_Date_/$d/g" Template.email | mail -s 'Information about your class' $email 
      ;; 

     *) 

      # Incorrect input 
      echo "Sorry that was not a correct choice.";; 

    esac 

    rm temp.csv 
    rm temp2.csv 
# End of file 
関連する問題