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

UNIX A telephone directory, teledir.txt, maintains records in the form name: num

ID: 3740840 • Letter: U

Question

UNIX

A telephone directory, teledir.txt, maintains records in the form name: number where number is of the form nnn-nnn-nnnn. Devise a shell script that accepts one or two arguments that could be: (i) the name or number. If it exists in the directory, then the line should be displayed (ii) both. The entry is displayed if it exists and is added if it doesn’t exist in the file. Note that the number will always be entered as a 10-digit string, so you’ll have to use the script developed in Exercise 13.18 to perform both conversion and validation

Explanation / Answer

Please find my answer:

name = $1
number = $2
set name_number = $1 $2
while read line
do
if [ `echo $line | grep -c "$name_number" ` -gt 0 ]
   then
   echo $name_number
else
   echo $name_number >> teledir.txt
fi
done<teledir.txt

Hope it helps. Please comment if you face difficulties to understand any of above code.

Please note some of the command used may be linux/unix distribution specific. If you're facing any problen while executing the script just comment it with your linux/unix version, I'll modify accordingly.