我正在尝试创建一个解压密码保护文件的脚本,密码是解压缩时我将获得的文件的名称
例如.
file1.zip contains file2.zip and it's password is file2.
file2.zip contains file3.zip and it's password is file3
如何解压缩file1.zip,并读取file2.zip的名称,以便在脚本中输入?
Here’s a screenshot of what I meant,我只需要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
现在我该如何让它循环?
解决方法:
如果您因任何原因没有也无法安装zipinfo,可以使用-Z选项解压缩来模仿它.要列出zip的内容,请使用unzip -Z1:
pw="$(unzip -Z1 file1.zip | cut -f1 -d'.')"
unzip -P "$pw" file1.zip
把它放到一个循环中:
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
或递归函数:
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"
-Z zipinfo(1) mode. If the first option on the command line is -Z, the remaining options are taken to be zipinfo(1) options. See the appropriate manual page for a description of these options.
-1 : list filenames only, one per line. This option excludes all others; headers, trailers and zipfile comments are never printed. It is intended for use in Unix shell scripts.
标签:linux,zip,password,shell-script
来源: https://codeday.me/bug/20190810/1635715.html