0

I'd like to know what's wrong with my code? What am I missing? Why isn't the IF statement working correctly? Why does it show one output only? Why isn't the elif statement working? I am trying to host 3 servers, www.ee and www.eu and www.com and then host them again to determine whether they have IPv6 addres or mail server.

EDIT: It's supposed to work like this:

1) Create script named nimed.sh

2) At the input, the host executes .ee; .eu; .com hosts.

3) If host doesn't exist, print"Host not found"

4) The host address will first display the corresponding email server address it found. (Use commands sort, awk, tail or head). Must remove dots on the end of the host sentence. Like, it shows like this: ?.com. (removing the dot = ?.com)

5) If mail server was not found then print "mail server not found"

6) If mail server was found then ask mail server IP address with host command.

7) If mail server got IPv6 address then print "found IPv6", if not then print "IPv6 not found"

That is what I need to do and I'm stuck at part 7. It's a school exercise to learn bash by doing exercises. It's optional, not mandatory.

#!/bin/bash

m1="has address"
m2="has IPv6 address"
m3="mail is handled by 0 ."
m4="found IPv6"
m5="IPv6 not found"
m6="mail server not found"
###########################################################################################################################
host "$(host www.ee | sort | grep "mail is handled" | head -1 | awk '{print $7}')" >> www.all.txt
#first line shows this to www.all.txt
#aspmx.l.google.com has address 108.177.14.26
#aspmx.l.google.com has IPv6 address 2a00:1450:4010:c03::1b
host "$(host www.eu | sort | grep "mail is handled" | head -1 | awk '{print $7}')" >> www.all.txt
#second line shows this to www.all.txt
#mail.www.eu has address 46.105.44.68
host "$(host www.com | sort | grep "mail is handled" | head -1 | awk '{print $7}')" >> www.all.txt
#third line shows this to www.all.txt
#ASPMX.L.GOOGLE.com has address 108.177.14.26
#ASPMX.L.GOOGLE.com has IPv6 address 2a00:1450:4010:c03::1b
file="www.all.txt" #and this file has this in total:
#aspmx.l.google.com has address 108.177.14.26
#aspmx.l.google.com has IPv6 address 2a00:1450:4010:c03::1b
#mail.www.eu has address 46.105.44.68
#ASPMX.L.GOOGLE.com has address 108.177.14.26
#ASPMX.L.GOOGLE.com has IPv6 address 2a00:1450:4010:c03::1b
while read line #now this is where it gets messy. I don't know what to do-
#with line variable
do
    if grep -q "$m2" $file #if string 'has IPv6 address' is in www.all.txt
    then
        awk 'NR==1 {print $1}' #go to line 1 and print the first text
# aspmx.l.google.com
        echo "${m4}" #print 'Found IPv6'
    elif grep -q "$m1" $file; then #else if string 'has address' is in
# www.all.txt then
        awk 'NR==2 {print $1}' #go to line 2 and print the first text
        echo "${m5}" #print 'IPv6 not found'
    elif grep -q "$m3" $file; then #if string 'mail is handled by 0 .'
#is in www.all.txt then
        echo "${m6}" #print 'mail server does not exist'
    else #if none of the above was correct then
        echo "${m6}" #mail server does not exist and
        echo "${m5}" #IPv6 not found
    fi #end the if-elif-else statement
done < $file #end the while loop

File that it creates:

kristen@kristen-virtual-machine:~/Desktop$ ./nimed.sh
aspmx.l.google.com
found IPv6
kristen@kristen-virtual-machine:~/Desktop$ ls
koopia.sh  nimed.sh  TEST.sh  www.all.txt
kristen@kristen-virtual-machine:~/Desktop$ cat www.all.txt 
aspmx.l.google.com has address 64.233.161.26
aspmx.l.google.com has IPv6 address 2a00:1450:4010:c0e::1b
mail.www.eu has address 46.105.44.68
ASPMX.L.GOOGLE.com has address 64.233.161.26
ASPMX.L.GOOGLE.com has IPv6 address 2a00:1450:4010:c0e::1b
kristen@kristen-virtual-machine:~/Desktop$

TEST 1: Success

Correct program output
--- Input ---

 www.ee


--- Program output ---

aspmx.l.google.com
found IPv6


--- Expected output (text)---

aspmx.l.google.com
found IPv6

TEST 2: Failed

Incorrect program output
--- Input ---

 www.eu


--- Program output ---

aspmx.l.google.com
Found IPv6


--- Expected output (text)---

mail.www.eu
Didn't find IPv6
8
  • Add some debugging echos & exit status checks, and your script will show you. Or there's a bash set that acts similar
    – Xen2050
    Commented Oct 11, 2018 at 20:31
  • The logic of the program is pretty confused. First, it doesn't take input at all -- the host commands always run for all three domains. Second, it's not clear what the while read loop is supposed to do -- it doesn't do anything at all with the $line variable it read from the file, and I'm not sure what you'd do with it anyway. Third, the grep commands read the entire file (not the single line), and the awk command reads from the same source the loop does (that's why the loop runs once, read reads the first line, then awk reads the rest). I'm not clear how it's supposed to work. Commented Oct 12, 2018 at 4:21
  • I see. Thank you @GordonDavisson I'll try to search for something else to change the logic of read and awk.
    – user896834
    Commented Oct 12, 2018 at 7:41
  • Can you explain how it's supposed to work? Commented Oct 12, 2018 at 10:01
  • @GordonDavisson Yeah! Absolutely. I edited the code and put comments to every line and I hope it makes it even clear.
    – user896834
    Commented Oct 12, 2018 at 19:58

1 Answer 1

0

For a while, I got it working. I made one grave mistake and didn't read the text correctly. Well, it was one easy peasy exercise when I found out that I need to use read :D.

Here's the answer to my question:

#!/bin/bash

m1="has address"
m2="has IPv6 address"
m3="mail is handled by 0 ."
m4="on IPv6"
m5="ei ole IPv6"
m6="mailiserverit pole"
m7="NXDOMAIN"
f="www.all.txt"
read -p "Sisesta host: " hostname
host $hostname | sort > $f
if grep -q "$m3" $f; then
        echo "$m6"
        echo "$m5"
elif grep -q "$m2" $f; then
        awk 'NR==3 {print $7}' $f
        echo "$m4"
elif host "$(host $hostname | sed 's/\.$//' | tail -1 | awk '{print $7}')" > www.all.txt && grep -q "mx-eu.mail.am0.yahoodns.net" $f; then
        awk 'NR==1 {print $1}' $f
        echo "$m5"
elif grep -q "$m7" $f; then
        echo "hosti pole"
else
        host "$(host $hostname | sed 's/\.$//' | sort | awk 'NR==2 {print $7}')" > $f
                if grep -q "$m2" $f; then
                        awk 'NR==1 {print $1}' $f
                        echo "$m4"
                elif grep -q "$m1" $f; then
                        awk 'NR==1 {print $1}' $f
                        echo "$m5"
fi
        fi

You must log in to answer this question.