Sunteți pe pagina 1din 2

#!

/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

S-ar putea să vă placă și