gpt4 book ai didi

linux - 意外标记附近的语法错误 `fi' 自定义脚本

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:03 24 4
gpt4 key购买 nike

我刚才测试时写了一个两部分的脚本,分别称为“bips.sh”和“chekdup.sh”,它显示:

bips.sh: line 17: syntax error near unexpected token `fi'
bips.sh: line 17: `fi'

这是脚本:

---bips.sh---
#!/bin/bash -x
# find broadcast ip's that reply with 30+ dupes.

# i decided to make this script into two sections. when running this make
# sure both parts are in the same directory.
if [ $# != 1 ]; then
echo "$0 <domain - ie: college.edu>"
else
host -l $1 | grep 'has address' | cut -d' ' -f4 > $1.ips
cat $1.ips | cut -d'.' -f1-3 | sort |\
awk '{ print echo ""$1".255" }' > $1.tmp
cat $1.tmp | uniq | awk '{ print "./chekdup.sh "$1"" }' > $1.ping
rm -f $1.ips $1.tmp
chmod 700 $1.ping
./$1.ping
rm $1.ping
fi

脚本的第二部分

---chekdup.sh---
#!/bin/bash -x
# this checks possible broadcast ip's for a given amount of icmp echo
# replies.
ping -c 2 $1 > $1.out
if
cat $1.out | grep dupl > /dev/null
then
export DUPES="`cat $1.out | grep dupl | cut -d'+' -f2 | cut -d' ' -f1`"
else
export DUPES=1
fi
if [ $DUPES -gt 30 ]; then
echo "$1 had $DUPES dupes" >> bips.results
rm -f $1.out
else
rm -f $1.out
fi

注意:我更新了代码,它总是报错

最佳答案

读那段代码太痛苦了,我不得不重写它

#!/bin/bash -x
# find broadcast ip's that reply with 30+ dupes.

if (( $# != 1 )); then
echo "$0 <domain>"
echo "example: $0 college.edu"
exit
fi

host -l $1 |
awk '
/has address/ {sub(/[0-9]+$/, "255", $NF); ips[$NF]}
END {for (ip in ips) print ip}
' |
sort |
while read ip; do
dupes=$( ping -c 2 $ip | grep -oP 'dupl.*\+\K\S+' )
if [[ $dupes && $dupes -gt 30 ]]; then
echo "$ip had $dupes dupes"
fi
done >> bips.results

关于linux - 意外标记附近的语法错误 `fi' 自定义脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16748502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com