mol*_*keh 6 linux password zip shell-script
我正在尝试制作一个脚本来解压缩受密码保护的文件,密码是我在解压时得到的文件名
例如。
file1.zip contains file2.zip and it's password is file2.
file2.zip contains file3.zip and it's password is file3
Run Code Online (Sandbox Code Playgroud)
我如何解压缩file1.zip,并读取它的名称file2.zip以便它可以输入到脚本中?
这是我的意思的屏幕截图,我只需要 bash 读取该输出即可知道新密码(在这种情况下,密码是 13811)。
这是我到目前为止所做的
#!/bin/bash
echo First zip name:
read firstfile
pw=$(zipinfo -1 $firstfile | cut -d. -f1)
nextfile=$(zipinfo -1 $firstfile)
unzip -P $pw $firstfile
rm $firstfile
nextfile=$firstfile
Run Code Online (Sandbox Code Playgroud)
现在我怎样才能让它做循环?
如果您没有并且zipinfo由于任何原因无法安装,您可以通过使用unzipwith-Z选项来模仿它。要列出 zip 的内容,请使用unzip -Z1:
pw="$(unzip -Z1 file1.zip | cut -f1 -d'.')"
unzip -P "$pw" file1.zip
Run Code Online (Sandbox Code Playgroud)
把它放到一个循环中:
zipfile="file1.zip"
while unzip -Z1 "$zipfile" | head -n1 | grep "\.zip$"; do
next_zipfile="$(unzip -Z1 "$zipfile" | head -n1)"
unzip -P "${next_zipfile%.*}" "$zipfile"
zipfile="$next_zipfile"
done
Run Code Online (Sandbox Code Playgroud)
或递归函数:
unzip_all() {
zipfile="$1"
next_zipfile="$(unzip -Z1 "$zipfile" | head -n1)"
if echo "$next_zipfile" | grep "\.zip$"; then
unzip -P "${next_zipfile%%.*}" "$zipfile"
unzip_all "$next_zipfile"
fi
}
unzip_all "file1.zip"
Run Code Online (Sandbox Code Playgroud)
-Z zipinfo(1) 模式。如果命令行上的第一个选项是 -Z,则其余选项将被视为 zipinfo(1) 选项。有关这些选项的说明,请参阅相应的手册页。
-1 :仅列出文件名,每行一个。此选项排除所有其他选项;永远不会打印标题、预告片和 zipfile 注释。它旨在用于 Unix shell 脚本。
询问zipinfozip 文件中列出的文件名,然后将其捕获以获取密码。使用该密码解压缩文件:
pw=$(zipinfo -1 file1.zip | cut -d. -f1)
unzip -P "$pw" file1.zip
Run Code Online (Sandbox Code Playgroud)
请注意,标志 tozipinfo是one而不是ell。
从Gilles 对类似问题的回答中大量借用,这是一个 bash 循环,它将提取受密码保护的嵌套 zip 文件,直到没有更多 zip 文件:
shopt -s nullglob
while set -- *.zip; [ $# -eq 1 ]
do
unzippw "$1" && rm -- "$1"
done
Run Code Online (Sandbox Code Playgroud)
我已将函数定义unzippw为上述zipinfo和unzip命令的包装器:
unzippw ()
{
local pw=$(zipinfo -1 "$1" | cut -d. -f1)
unzip -P "$pw" "$1"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4074 次 |
| 最近记录: |