A possible script - save this in a file called e.g. 7za_script.sh
#!/bin/bash
for file in *.7z
do
for password in pass1 pass2 pass3
do
7za x -oOUTPUTDIR -p"${password}" "${file}" && break
done
done
What does it do?
1. The outer loop for file in *.7z
We define the variable called file
to any entry in your current working directory that ends in .7z
. The *
hereby is a wildcard that is expanded to zero or any number of characters. *.7z
will thus match anythng that has a 7zip
suffix. The loop now runs once per match and we can refer to the match of the current loop run via $file
, i.e. calling a variable.
2. The inner loop for password in pass1 pass2 pass3
In this loop we define tha variable password
by explicitely stating three optional values pass1
, pass2
, and pass3
. The loop will run once for each value.
3. The 7za
-command
Try to extract the current file from our list with the given password. For each $file
all three $password
possibilities are tried. The order of nested loops is such that the inner loop will always be run fully before going to the next step in the outer loop.
4. The && break
part
break
will stop a loop run of the innermost loop. Thus if we call break
in the password loop after trying pass1
the pass2
and pass3
option will not even be tried. The question is when NOT to try other passwords. Simple: upon successfully extracting a file with pass1
the other passwords need not to be tried for that file.
Each command has an exit status
that is either 0
for success or any other number for fail
(the actual value of the number can give more information about what went wrong, but this depends on the individual program). man 7za
shows under DIAGNOSTICS
:
0 Normal (no errors or warnings detected)
1 Warning (Non fatal error(s)). For example, some files cannot be read during compressing, so they were not compressed
2 Fatal error
Thus if a file is successfully extracted, the exit code should be 0
and if a password does not unlock it, it should be 2
. You can get the exit status of a previous command by echo $?
. Just try echo $?
after running true
or false
(commands that either always succeed or always fail).
Now && command
means "run command if the previous command was successful (exit status = 0
). So what we do is: if $password
unlocked $file
(and no other error occured), we break
the password-loop, as there is no reason for trying the other passwords on that file. Instead we can now go on to the next file.
5. Running the script
Once you saved the script as e.g. 7za_script.sh
and wrote the passwords in there as you need them, you can run it via bash 7za_script.sh
OR make it executable with chmod +x 7za_script.sh
and run it via ./7za_script.sh
.
6. Notes
- The script is very simplistic, but just meant as a basic start for your scripting career.
- Move it to your desired directory and execute it.
- It will not differ between a file called
1.7za
or a directory 2.7za
(that is obviously not a zipped thing at all but just shares the name). Testing this would be the next step.
- It is very unsafe to store passwords in a clear text file, but it seems to be a low security problem in the first place.
- Quoting
"
and calling variables as ${var}
instead of $var
make scripts more stable in case of e.g. spaces in filenames. I'll not go more into detail here but leave the keywords: hard quotes, soft quotes, shell variables
&&
and ||
are called OPERATORS
- Take this as a start, try to understand what it does and enjoy scripting.
for
-loop for all files, an innerfor
-loop for all passwords.break
the inner loop, if the extraction succeeds:7za x -oDIR -p"${passw}" "${file}" && break
. a && b = if successful termination a-command, do b-command. Please also try to edit your question to make your code readable. Markdown helpbash
commands and operators:for ... do ; done
(nested),break
,&&
,||
.