linux 循环解压,linux – Bash循环解压缩passworded文件脚本

我正在尝试创建一个解压密码保护文件的脚本,密码是解压缩时我将获得的文件的名称

例如.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值