bash error checking problems[solved]

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data: I am trying to make a script called showtime that displays the current time for a given city.
    The problem is on the error checking part im trying to make the script search a directory called zoneinfo to see if the region or city the user entered exist.

  2. Relevant commands, code, scripts, algorithms:
    # Declear Varables
    region=$1
    city=$2
    #Checking if region exist in the database
    region=$(ls ~/zoneinfo| grep '$1')

  3. The attempts at a solution (include all code and scripts):
    # Declear Varables
    region=$1
    city=$2
    #Checking if region exist in the database
    region=$(ls ~/zoneinfo| grep '$1')

if [ '$1' == "" ]; then
echo $1 does not exist
exit 2
fi

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):Seneca College professor name John selmy/course name OPS 435

your website wont let me post the url to the courses sit for some reason??

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

use double quotes, while using the grep

 
$ a=TEST
$ echo "TEST" | grep '$a'   #it will not give output
$ echo "TEST" | grep "$a" 
TEST

I tried that but it won't display the error message to the user

heres my code now

#Checking if region exist in the database
region=$(ls zoneinfo| grep "$1")
echo $region
if [[ $1 = "" ]]; then
    echo $1 does not exist
    exit 2
fi

Just teasing a bit...
What if you change directory ( see me coming?...)
what would be the output of region=$(ls zoneinfo| grep "$1") ?

...
assuming zoneinfo is in $HOME:

region=$(cat ~/zoneinfo | grep "$1")
echo $region
if [ "$region" = "" ]
then
    echo $1 does not exist
    exit 2
fi
region=$(ls ~/zoneinfo|grep $1| cut -d"/" -f1 )

with that block of code im trying to make the script search for a region that a user entered which is $1 and see if the region that user entered exists or not

if [[ "$1" = "" ]]; then
    echo $1 does not exist
    exit 2
fi

but when I run my script showtime with a wrong region it does not show the error message.

ant:/home/vbe/test $ zz Autralia 

Autralia does not exist
ant:/home/vbe/test $ zz Australia
Australia
ant:/home/vbe/test $ more zz
#region=$(cat zoneinfo | grep "$1") # Now I understood what you are up to
region=$(ls ~/zoneinfo|grep $1| cut -d"/" -f1)   # ln -s to zoneinfo directory...in $HOME
echo $region
if [ "$region" = "" ]
then
    echo $1 does not exist
    exit 2
fi

zz: END

$1 empty for me means nothing was entered... You say in your last post you are looking for a region...

nvm i fixed the problem last week :slight_smile: