【问题标题】:Problems with head -n/reading on line of a file头和/在线读取文件的问题
【发布时间】:2017-07-16 16:00:12
【问题描述】:
    for ((zeile=1;zeile<=1284;zeile++));
do
  sleep 1.5
   export accountList=$(cat /root/pawnedDog/database/accountList | head -n${zeile})

   wget https://haveibeenpwned.com/api/v2/breachedaccount/${accountList}@gmail.com-P /root/pawnedDog/pawnedList/

done

我想制作一个 shell 脚本来检查整个电子邮件地址列表是否被黑客入侵。如果我运行脚本 wget 命令开始下载 gmail.com。我究竟做错了什么?

输出:

--2017-02-25 22:52:52--  https://haveibeenpwned.com/api/v2/breachedaccount/Adanna-Marie.Feder@gmail.com
Resolving haveibeenpwned.com (haveibeenpwned.com)... 2400:cb00:2048:1::6814:5b3a, 2400:cb00:2048:1::6814:5c3a, 104.20.92.58, ...
Connecting to haveibeenpwned.com (haveibeenpwned.com)|2400:cb00:2048:1::6814:5b3a|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2017-02-25 22:52:53 ERROR 404: Not Found.

--2017-02-25 22:52:54--  https://haveibeenpwned.com/api/v2/breachedaccount/Adanna-Marie.Feder
Resolving haveibeenpwned.com (haveibeenpwned.com)... 2400:cb00:2048:1::6814:5c3a, 2400:cb00:2048:1::6814:5b3a, 104.20.91.58, ...
Connecting to haveibeenpwned.com (haveibeenpwned.com)|2400:cb00:2048:1::6814:5c3a|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2017-02-25 22:52:55 ERROR 404: Not Found.

--2017-02-25 22:52:55--  http://Ada.Petriconi@gmail.com/
Resolving gmail.com (gmail.com)... 2a00:1450:4001:815::2005, 172.217.16.165
Connecting to gmail.com (gmail.com)|2a00:1450:4001:815::2005|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.google.com/gmail/ [following]
--2017-02-25 22:52:55--  https://www.google.com/gmail/
Resolving www.google.com (www.google.com)... 2a00:1450:4013:c05::69, 64.15.112.93, 64.15.112.89, ...
Connecting to www.google.com (www.google.com)|2a00:1450:4013:c05::69|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://mail.google.com/mail/ [following]
--2017-02-25 22:52:55--  https://mail.google.com/mail/
Resolving mail.google.com (mail.google.com)... 2a00:1450:4001:81c::2005, 172.217.16.165
Connecting to mail.google.com (mail.google.com)|2a00:1450:4001:81c::2005|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=googlemail&emr=1&osid=1 [following]
--2017-02-25 22:52:55--  https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=googlemail&emr=1&osid=1
Resolving accounts.google.com (accounts.google.com)... 2a00:1450:4001:819::200d, 172.217.23.141
Connecting to accounts.google.com (accounts.google.com)|2a00:1450:4001:819::200d|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘/root/pawnedDog/pawnedList/index.html.14’

/root/pawnedDog/pawnedList/index.html.14                 [ <=>                                                                                                                    ]  58.38K  --.-KB/s   in 0.05s

2017-02-25 22:52:55 (1.11 MB/s) - ‘/root/pawnedDog/pawnedList/index.html.14’ saved [59777]

FINISHED --2017-02-25 22:52:55--
Total wall clock time: 1.3s
Downloaded: 1 files, 58K in 0.05s (1.11 MB/s)

这是我的第一篇文章,我的英语不是很好。

【问题讨论】:

    标签: bash shell ubuntu debian wget


    【解决方案1】:

    看起来您的文件/root/pawnedDog/database/accountList 每行有一个用户名。因此,您可以更有效地使用read 重写循环:

    while read -r account; do
      sleep 1.5
      wget https://haveibeenpwned.com/api/v2/breachedaccount/${account}@gmail.com -P /root/pawnedDog/pawnedList/
    done < /root/pawnedDog/database/accountList
    

    您的代码的问题是 head -n${zeile} 会导致获取多个帐户(第一次迭代中的 1 个帐户,第二次迭代中的 2 个,以此类推)。您可以将管道编写为:

    accountList=$(cat /root/pawnedDog/database/accountList | head -n${zeile}) | tail -1)
    

    但是,这不如在 read 循环中每次迭代读取一行高效。

    【讨论】:

    • 你太棒了!谢谢!出于好奇:您能解释一下为什么需要尾部 -1 部分吗?
    • head -n 为您提供文件的前 n 行。 tail -1 会给你最后一行。 head -n file | tail -1 会给你第 n 行。
    • 要获取文件的第 n 行,您可以使用 sed -n ${zeile}p,它是一步完成而不是两个(管道越少越好),并允许您从行中提取特定部分如果你使用 s 命令。
    猜你喜欢
    • 2016-04-18
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多