0

For a uni-assignment I need to write a while-loop for BASH-shell (on windows) that tries different passwords line by line from a given .txt-file and enter them to unzip a given .zip-Archive.

I know how to unzip archives and I know how to echo the .txt-file contents line by line, but can't figure out how to combine them:

#1

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

#2

unzip -P $pw archive.zip

2 Answers2

0

In bash there are several ways to accomplish this task, like the follow:

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

Then call the unzip command...

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • seems to me like OP wants an automatic looping method. Your solution involves interaction and there is simply no loop, or unzip. kindly consider providing a more suitable answer that isn't [the same as this](https://stackoverflow.com/a/67618292/13982210) – kevinnls May 20 '21 at 11:03
  • 2
    other side note, UPPERCASE user-defined variables in shell are not recommended to avoid overwriting or conflicting with environment variables – kevinnls May 20 '21 at 11:05
0

I would try to the following way:

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

You can read about read -r option here.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • 1
    +redirct passwords.txt as input to the while loop `done < passwords.txt` – kevinnls May 20 '21 at 10:54
  • 4
    Please fix your script by adding the `-r` flag to `read`: `read -r pw`, and by adding double quotes when expanding a variable value in `unzip -P "$pw" archive.zip` and by testing the return value of the command directly rather than addressing the `$?` variable: `if unzip -P "$pw" archive.zip; then`… – Léa Gris May 20 '21 at 12:38
  • 1
    See also [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee May 21 '21 at 05:51
  • Thank you for the comments. I have edited the answer as per suggestions. – kiner_shah May 22 '21 at 05:52