bash的逻辑方法

bash的逻辑方法,bash,Bash,直到本节为止。下面的行和名称参数无法匹配。应该如何改变 #!/bin/bash echo -n "Enter the domain name > " read name dig -t ns "$name" | cut -d ";" -f3 | cut -d ":" -f2| grep ns | cut -f6 > registerfile.txt; cat registerfile.txt | while read line; do dig axfr "@$line" "$name"

直到本节为止。下面的行和名称参数无法匹配。应该如何改变

#!/bin/bash
echo -n "Enter the domain name > "
read name
dig -t ns "$name" | cut -d ";" -f3 | cut -d ":" -f2| grep ns | cut -f6 > registerfile.txt;
cat registerfile.txt | while read line; do dig axfr "@$line" "$name"; done | cut -d"." -f-4 > nmap.txt

目前还不清楚到底哪里出了问题,但这里有一个重构,希望至少能将您推向正确的方向

cat nmap.txt | while read line; do if [ "$line" == "$name" ]; then host "$line"; fi; done > ping.txt 
cat ping.txt | cut -d " " -f4  | while read line; do if  [[ "$line" =~ ^[0-9]+$ ]]; then nmap -sS "$line";fi ;done
如果[“$line”=“$name”],您在评论中的评论是
;然后宿主“$line”;fi
不起作用表明那里的逻辑有点错误。它目前检查每一行是否与原始域名相同,然后在这些情况下反复查找,这似乎是一件奇怪的事情;但考虑到只有代码和“不起作用”,很难说它真正应该完成什么。如果你真的想要别的东西,你需要更具体地说明你需要什么。也许你真的在寻找类似的东西

#!/bin/bash
read -p "Enter the domain name > " name
dig +short -t ns "$name" |
tee registerfile.txt |
while read line; do
    dig axfr "@$line" "$name"
done |
cut -d"." -f-4 |
tee nmap.txt |
while read line; do 
    if [ "$line" = "$name" ]; then
         host "$line"
    fi
done > ping.txt 
cut -d " " -f4 ping.txt | 
grep -E '^[0-9]+$' |
xargs -r -n 1 nmap -sS

使用多个静态命名文件是一种反模式;显然,如果这些文件没有外部用途,只需取出
tee
命令,运行整个管道,而不需要中间的输出文件。如果您确实需要这些文件,在每次运行时覆盖它们似乎有问题——可能会在文件名中添加一个唯一的日期戳后缀?

如果[“$line==“$name”];
您在
$line
中缺少一个
谢谢,还有一个问题:cat nmap.txt;在阅读第行时;如果[“$line”==“$name”],则执行此操作;然后宿主“$line”;fi;done>ping.txt不起作用,也就是说,它不会给出任何逻辑错误。但是,如何纠正逻辑方法?您可能想对此做些什么。长字符串
cut
grep
似乎是一种糟糕而脆弱的方式来表示
dig+short-tns“$name”
不清楚问题的标题与内容的关系。一个好问题的标题是该问题独有的,因此有类似问题的人可以找到它(没有类似问题的人不需要点击来判断它或它的答案是否对他们有帮助)。(曾几何时,一个显然对任何人都没有帮助的问题可以简单地结束;尽管规则从那时起发生了一些变化,但目标仍然是建立一个可重用的知识库,其中的问题和答案将有助于其他人)。
... tee nmap.txt |
# Extract the lines which contain $name at the end
grep "\.$name\$" |
xargs -n 1 dig +short |
tee ping.txt |
grep -E '^[0-9]+$' ...