【问题标题】:Entering lines into password promt [duplicate]在密码提示符中输入行[重复]
【发布时间】:2021-08-09 13:37:59
【问题描述】:

对于 uni-assignment,我需要为 BASH-shell(在 Windows 上)编写一个 while 循环,从给定的 .txt 文件中逐行尝试不同的密码 em> 并输入它们以解压缩给定的 .zip-Archive

我知道如何解压缩档案,我知道如何逐行回显 .txt-file 内容,但不知道如何组合它们:

#1

while read pw
    do echo "$pw"
done < passwords.txt

#2

unzip -P $pw archive.zip

【问题讨论】:

  • 那么什么是硬的?将unzip 替换为echo

标签: bash while-loop command-line git-bash unzip


【解决方案1】:

我会尝试以下方式:

while read -r pw
do
    if unzip -P "$pw" archive.zip
    then
        break
    fi
done < passwords.txt

你可以阅读read -r选项here

【讨论】:

  • +redirct passwords.txt 作为 while 循环的输入 done &lt; passwords.txt
  • 请通过将-r 标志添加到read:read -r pw 并在扩展unzip -P "$pw" archive.zip 中的变量值时添加双引号并测试返回值来修复您的脚本命令直接而不是寻址$? 变量:if unzip -P "$pw" archive.zip; then...
  • 感谢 cmets。我已根据建议编辑了答案。
【解决方案2】:

在 bash 中有几种方法可以完成这项任务,如下所示:

echo -n Please enter the password: 
read -s PASSWORD
echo "Debug: your entered the passowrd \"${PASSWORD}\"."

然后调用解压命令...

【讨论】:

  • 在我看来,OP 想要一种自动循环方法。您的解决方案涉及交互,根本没有循环或解压缩。请考虑提供一个不是the same as this 的更合适的答案
  • 其他注意事项,不建议在shell中使用大写用户定义的变量,以免覆盖或与环境变量冲突
猜你喜欢
  • 2011-10-17
  • 2015-02-21
  • 2019-02-05
  • 1970-01-01
  • 2014-09-16
  • 2012-07-25
  • 2018-12-02
  • 2022-01-25
  • 2020-06-01
相关资源
最近更新 更多