Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[UNIX] /bin/bash program to search a medication list and produce a report as req

ID: 3839128 • Letter: #

Question

[UNIX] /bin/bash program to search a medication list and produce a report as requested by the user.

the medslist file is located at the workign directory under name "medslist"

Sample Output

The file medslist is a flat plain text file with the following format shown in the table below Columns Data field 01-04 Category 05 12 Medication Code 13 25 Generic Name 20 39 Dose 40 46 On-Hand (inventory) Example of the medslist fie... (The first two lines represent columns numbers, not in file) 12345678901234567890123456 789 1 45678901 56789012 34567890 comm A6314 ifosfamide 30 home5341209 urokinase 6314 37 etc

Explanation / Answer

Code:

#!/bin/bash

# search medicine list and generate a report
# script name: search.sh

# Loop to ask medication code and generic name or dose
# Enter 'ZZZ' to quit
while :
do
   # taking medication code from the user
   echo -n "Enter Medication code? "
   read mcode  
   # converting medication code to upper case for comparision if necessary
   mcode=$(echo $mcode|tr 'a-z' 'A-Z')
   # if mcode is ZZZ, quit from the outer while loop
   if [ "$mcode" == 'ZZZ' ]
   then
       break
   fi
   # loop to ensure generic name or dose is passed correctly
   # if generic name is 'G' or 'D' this loop terminates
   while :
   do
       # taking generic name or dose as input from user
       echo -n "See Generic Name (G/g) or Dose (D/d) ? "
       read gname
       # converting gname to upper for comparision in if condition below
       gname=$(echo $gname|tr 'a-z' 'A-Z')
       if [ "$gname" == 'G' -o "$gname" == 'D' ]
       then
           break
       else
           echo "Please enter only G or D."
       fi
   done

   # grepping given mcode in the medslist file and redirecting to file /tmp/result
   grep $mcode medslist>/tmp/result
   # traversing through /tmp/result file and print required category and medcode
   while read line
   do
       category=$(echo $line|cut -c1-4)
   medcode=$(echo $line|cut -c5-12)
   genname=$(echo $line|cut -c13-25)
   dose=$(echo $line|cut -c26-39)
   inventory=$(echo $line|cut -c40-46)
       echo "$category $medcode"
   done</tmp/result
   # below condition will be true if medication code is wrong
   if [ ! -s /tmp/result ]
   then
       echo "No such medication code"
   fi
done

NOTE: 'ZZZ' is the string you need to pass to quit out from the loop

Execution and output:
186590cb0725:Shell bonkv$ ./search.sh
Enter Medication code? foo
See Generic Name (G/g) or Dose (D/d) ? g
No such medication code
Enter Medication code? 123
See Generic Name (G/g) or Dose (D/d) ? g
1234 56789012
Enter Medication code? 6314
See Generic Name (G/g) or Dose (D/d) ? g
comm A6314 if
home 5341209
Enter Medication code? 23
See Generic Name (G/g) or Dose (D/d) ? j
Please enter only G or D.
See Generic Name (G/g) or Dose (D/d) ? d
1234 56789012
Enter Medication code? ZZZ

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote