To design a shell that accepts an input string which is a state name, and looks for all the universities that are in that state. If found, it displays all the universities as output, otherwise it displays an error message like “xxx was not found in the file”. Here xxx is the input string. (Hint: This can be done by redirecting the search results to a file and then checking whether the file is empty or not). For example, if the input string is “NSW”, the output should be a list of all the universities in NSW. If the input is “AUS”, an error message should be displayed, saying that “AUS was not found in the file”.
Here is my code:
#!/bin/sh
echo "Please enter State of Uni (e.g NSW ; NAME MUST BE UPPER CASE)"
read State
if [ -n $State ]
then
grep "$State" Aus-Uni.txt
else
echo "$State was not found in the file"
fi
exit
There is no false statement popping up even the string that I entered was not found in the file. Somehow the true statement is roughly executed.
if [ -z "$State" ]; then echo "You didn't type a state abbreviation" >&2; elif ! grep "$State" Aus-Uni.txt; then echo "$State was not found in the file Aus-Uni.txt" >&2; fi... which reports to standard error, and spots when AUS is not found, and other improvements. You report the the state is not found only when the name given is empty — not quite what was requested.grep "$1" Aus-Uni.txt || echo "$1 was not found in Aus-Uni.txt" >&2-n $Statetest and executegrep, theelsecode belonging to the-n $Statetest will not be reached. And please put quotes around$State.