我在这个 Bash 脚本中做错了什么?

我在这个 Bash 脚本中做错了什么?

我想知道我的代码有什么问题?我遗漏了什么?为什么语句不能IF正常工作?为什么它只显示一个输出?为什么语句不起作用elif?我正在尝试托管 3 个服务器,www.ee、www.eu 和 www.com,然后再次托管它们以确定它们是否具有 IPv6 地址或邮件服务器。

编辑:它应该像这样工作:

1)创建名为nimed.sh的脚本

2) 在输入处,主机执行.ee;.eu;.com 主机。

3)如果主机不存在,则打印"Host not found"

4)主机地址会先显示找到的对应邮件服务器地址。。(Use commands sort, awk, tail or head)必须去掉主机句尾的点。比如,它显示为:?.com。(去掉点 = ?.com)

5)如果没有找到邮件服务器则打印"mail server not found"

6)如果找到邮件服务器,则使用 host 命令询问邮件服务器 IP 地址。

7)如果邮件服务器有 IPv6 地址则打印"found IPv6",如果没有则打印"IPv6 not found"

这就是我需要做的,但我卡在第 7 部分。这是通过做练习来学习 bash 的学校练习。它是可选的,不是强制性的。

#!/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

它创建的文件:

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$

测试 1:成功

Correct program output
--- Input ---

 www.ee


--- Program output ---

aspmx.l.google.com
found IPv6


--- Expected output (text)---

aspmx.l.google.com
found IPv6

测试 2:失败

Incorrect program output
--- Input ---

 www.eu


--- Program output ---

aspmx.l.google.com
Found IPv6


--- Expected output (text)---

mail.www.eu
Didn't find IPv6

答案1

有一段时间,我让它工作了。我犯了一个严重的错误,没有正确阅读文本。好吧,这是一个简单的练习,当我发现我需要使用read:D。

以下是我的问题的答案:

#!/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

相关内容