【问题标题】:Create separate file for each result为每个结果创建单独的文件
【发布时间】:2017-03-13 16:23:29
【问题描述】:

我有一个 bash 脚本如下:

SITE_LIST=/home/wps/sites.txt 

for file in `cat $SITE_LIST`
    do
        /usr/bin/wpscan --update --no-color --url $file --disable-tls-checks --follow-redirection --enumerate utp > "$file".txt
    done

当我运行这个脚本时,我得到以下输出:

./wpscan_script_2: line 15: www.need_help.com: No such file or directory
./wpscan_script_2: line 15: www.need_help.com: No such file or directory
./wpscan_script_2: line 15: www.need_help.com: No such file or directory
./wpscan_script_2: line 15: www.need_help.com: No such file or directory
./wpscan_script_2: line 15: www.need_help.com: No such file or directory
./wpscan_script_2: line 15: www.need_help.com: No such file or directory

注意为了安全起见,我用网站 www.need_help 替换了实际网站。这不是实际网站的名称

我正在尝试将每个结果通过管道传输到其自己的文件中。我目前在这方面没有成功。

【问题讨论】:

  • wpscan_script_2第15行的内容是什么?
  • @user000001 /usr/bin/wpscan --update --no-color --url $file --disable-tls-checks --follow-redirection --enumerate utp > "$file" .txt 我删除了一些回声语句。对此感到抱歉
  • 你应该总是用双引号引起来shell变量...--url "$file"
  • 不要使用for循环遍历文件;见Bash FAQ 001
  • shellcheck.net 会标记未加引号的参数和 for 循环。

标签: bash shell for-loop


【解决方案1】:

我认为您的文件包含以下行的错误:www.need_help.com/xyzxyz 正在做任何事情,没关系)

这意味着最终"$file".txt 会扩展为www.need_help.com/xyz.txt

因此,您的脚本尝试将输出写入文件夹 www.need_help.com 中的文件 xyz.txt,但由于该文件夹不存在而失败。

错误消息令人困惑但准确:www.need_help.com: No such file or directory

您可能希望将所有不需要的字符替换为其他字符,例如仅保留字母数字和点

for url in `cat "$SITE_LIST"`
    do
        file="$(printf '%s' "$url" | sed 's/[^A-Za-z0-9\.]/_/g')"
        /usr/bin/wpscan --update --no-color --url "$url" --disable-tls-checks --follow-redirection --enumerate utp > "$file".txt
    done

如果有很多替代品符合您的口味,您可以修改 sed 正则表达式。但它至少应该将所有/ 替换为例如:sed 's#/#-#g'

编辑:错过了您也使用 $file 作为 url

【讨论】:

    猜你喜欢
    • 2012-04-24
    • 2012-12-10
    • 1970-01-01
    • 2015-01-24
    • 2016-06-13
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多