linux – 使用Wget的Shell脚本 – 如果else嵌套在for循环中

我正在尝试创建一个shell脚本来读取下载URL列表,以查找它们是否仍处于活动状态.我不确定我当前的脚本有什么问题,(我是新手),任何指针都会有很大的帮助!

user @ pc:〜/ test#cat sites.list

http://www.google.com/images/srpr/logo3w.png
http://www.google.com/doesnt.exist
notasite

脚本:

#!/bin/bash
for i in `cat sites.list`
do
wget --spider $i -b
if grep --quiet "200 OK" wget-log; then
echo $i >> ok.txt
else
echo $i >> notok.txt
fi
rm wget-log
done

按原样,脚本将所有内容输出到notok.txt – (第一个google网站应该转到ok.txt).
但如果我跑:

wget --spider http://www.google.com/images/srpr/logo3w.png -b

然后做:

grep "200 OK" wget-log

它没有任何问题地抓住字符串.我用语法做了什么noob错误?谢谢m8s!

最佳答案
-b选项是将wget发送到后台,所以你在wget完成之前就做了grep.

尝试不使用-b选项:

if wget --spider $i 2>&1 | grep --quiet "200 OK" ; then

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...