0

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.

7
  • Double check what your if statement is checking. Commented Aug 27, 2016 at 18:57
  • 3
    Please take a look: shellcheck.net Commented Aug 27, 2016 at 18:59
  • Note that you could use: 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. Commented Aug 27, 2016 at 20:45
  • Why are you taking the state as input instead of as an argument? Just pass it as an argument and your entire script becomes grep "$1" Aus-Uni.txt || echo "$1 was not found in Aus-Uni.txt" >&2 Commented Aug 27, 2016 at 20:46
  • When you passed the -n $State test and execute grep, the else code belonging to the -n $State test will not be reached. And please put quotes around $State. Commented Aug 27, 2016 at 21:39

2 Answers 2

2

Firstly, you've no way to check whether the user input is compliant with your requirement that it should be all upper-case.

You could use [ shell param expansion ] to convert the input to all-uppercase before processing, well, something like :

echo "Please enter State of Uni (e.g NSW)"
read State
State="${State^^}" # Check ${parameter^^pattern} in the link

Change

if [ -n $State ]

to

if [ -n "$State" ] 
# You need to double-quote the arguments for n to work
# You can't use single quotes though because variable expansion won't happen inside single quotes
Sign up to request clarification or add additional context in comments.

1 Comment

The OP cannot use ${State^^} because the #! line says sh, not bash (even though the question is tagged bash). grep -i instead?
2

This only checks whether the string is nonempty

[[ -n $State ]]

The grep runs if the check succeeds - but the success of grep is not checked

Try this

if [[ -n $State ]]; then
  if ! grep "$State" Aus-Uni.txt; then
    echo "$State was not found in the file"
    exit 2
  fi
else
  echo "State is empty"
  exit 1
fi

2 Comments

hmm, also mention the reason for introducing [[..]] :)
Error messages should go to stderr, not stdout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.