2

I have this bash script to unzip files with different passwords. But it decompresses indefinitely and damages the decompressed files

PASS="passfoo passbar passfoobar"
LIST=$(ls -1 *.{zip,7z,7z.001,rar})
for password in $PASS; do
  OIFS="$IFS"
  IFS=$'\n'
  for i in $LIST
  do
      echo "$password"
      7z x -y -p"$password" "$i" -aoa
      if [ $? -eq 0 ]; then
     continue
    fi
   done
   IFS="$OIFS"
  done

To temporarily fix it I have had to create a loop for each password.

What I want is that when the script matches a password, the script won't test the others, and if there is no match with any password that tries to decompress it normally (if it does not have a password) or that shows an error (if the file has a password but it is not in the variable) without stopping to decompress the other files. Thk

PD: Keep in mind that files to decompress sometimes come with names with space and other non-ASCII characters, or are files compressed by parts

1

1 Answer 1

2

Not sure if the continue is the correct action inside the inner loop but I have adjusted your code to this.

#!/usr/bin/env bash

shopt -s extglob nullglob

passw=(
  passfoo
  passbar
  passfoobar
  banana
  chocolate
  whiskey
  vodka
  icecream
)

for f in *.@(zip|7z|7z.001|rar); do
  for p in "${passw[@]}"; do
    if 7z x -y -p"$p" "$f" -aoa; then
      break
    fi
  done
done
  • the shopt -s enables the shell options that are not enabled by default, shopt -u to disable the options.

  • extglob a feature that let's you do the *.@(zip|7z|rar) to match different file extensions. which expands to *.zip *.7z and *.rar and expands to all the files in the directory that ends in that particular extension (if there are files) not sure about the order though.

  • nullglob If there are no files ending in the pattern *.@(zip|7z|rar) nothing will be evaluated so it will be empty, if not enabled the glob will expand to a literal *.rar or *.7z or *.zip which will cause an error to your script.

Edit: As per the OP's @ajcg , I have change the continue to break so it works as expected.

5
  • I don't use 7z so I can't really say anything about that, I just adjusted your script is all.
    – Jetchisel
    Apr 30, 2020 at 0:03
  • 1
    Please modify the loop and change "continue" to "break". That's how it works. To select your answer as correct. Thanks for your help
    – acgbox
    Apr 30, 2020 at 0:04
  • 1
    Well at least we both learn something from this...
    – Jetchisel
    Apr 30, 2020 at 0:06
  • Could you explain the purpose of "shopt -s extglob nullglob"? Thank you
    – acgbox
    Apr 30, 2020 at 0:17
  • 1
    I have added an explanation, see the section of the bash manual about those things.
    – Jetchisel
    Apr 30, 2020 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.