从bash关联数组键解压文件列表
我的bash脚本创建一个以文件为键的关联数组从bash关联数组键解压文件列表,bash,associative-array,unzip,Bash,Associative Array,Unzip,我的bash脚本创建一个以文件为键的关联数组 declare -A fileInFolder for f in "${zipFolder}/"*".zip"; do read -r fileInFolder[$f] _ < <(md5sum "$f") done ... code for removing some entry in fileInFolder ... unzip -qqc "${!fileInFolder[@]}" 我尝试使用7z,但我没有找到方法提供多
declare -A fileInFolder
for f in "${zipFolder}/"*".zip"; do
read -r fileInFolder[$f] _ < <(md5sum "$f")
done
... code for removing some entry in fileInFolder ...
unzip -qqc "${!fileInFolder[@]}"
我尝试使用7z
,但我没有找到方法提供多个zip文件作为输入(如果我的理解正确,请使用-ai
选项…前言
这个答案基本上归结为一个unzip
命令无法做到这一点,假设您知道可以将unzip-qqqc“$f”
放入您在问题中编写的for
循环中,并且出于某种原因您不想这样做
我的回答
对于所有文件,您没有收到错误;相反,从上的第二个文件中,所有文件都会出现错误
只需尝试以下方法
unzip -qqc file1.zip file2.zip
你会得到错误
caution: filename not matched: file2.zip
这正是你得到的
从解压的man
页面
看起来您只能在命令行上提供一个zip
文件
实际上不完全是这样,因为您可以在命令行上指定更多的zip
文件,但要做到这一点,您必须依靠unzip
自己解释自己的命令行的方式;这部分模拟了外壳,但它所能做的一切都列在man
页面中:
因此,从技术上讲,您面临的问题与您在7z
- 忽略关联数组存储zip文件MD5的原因
- 正如@Enrico Maria De Angelis指出的,
unzip
每次调用只接受一个zip文件参数。因此,您不能将关联数组文件名索引扩展为单个调用unzip
的参数
我提出这一解决办法:
#/usr/bin/env bash
#如果没有匹配项,则不希望解压缩模式名称
shopt-s nullglob
声明-文件文件夹
对于“${zipFolder}/”*“.zip”中的f;做
#将zip文件的MD5存储到assoc array fileInFolder中
#关键字:zip文件名
#值:zip文件的md5sum
从您的描述(unzip
give me)来看,awk
的使用似乎根本不相关,因为错误发生在unzip
中。这是正确的吗?你可以直接用你的建议执行read-r fileInFolder[$f].<@LéaGris简化代码。我添加了一个替代方法,只为所有zip文件调用md5sum一次。只需循环数组索引,并将循环传递到awk脚本:for f in“${!fileInFolder[@]}”;请解压缩-qqc“$f”;完成| awk-f script.awk
我不认为OP在寻找这个解决方案,否则为什么要首先创建关联数组?他可以把unzip-qqqc“$f”
放在循环中,他已经在问题中写过了。我猜@Nathanaël将zip文件的MD5存储在assoc数组中是为了其他没有提到的目的。他确实可以直接从同一个循环解压到标准输出,以填充assoc数组,并将同一个循环传递到awk脚本。@LéaGris将循环传递到awk
完成了这些技巧!无法向上投票评论,但这是我的解决方案。
caution: filename not matched: file2.zip
SYNOPSIS
unzip [-Z] [-cflptTuvz[abjnoqsCDKLMUVWX$/:^]] file[.zip] [file(s) ...] [-x xfile(s) ...] [-d exdir]```
ARGUMENTS
file[.zip]
Path of the ZIP archive(s). If the file specification is a wildcard, each matching file is processed in an order determined by the
operating system (or file system). Only the filename can be a wildcard; the path itself cannot. Wildcard expressions are similar to
those supported in commonly used Unix shells (sh, ksh, csh) and may contain:
* matches a sequence of 0 or more characters
? matches exactly 1 character
[...] matches any single character found inside the brackets; ranges are specified by a beginning character, a hyphen, and an ending
character. If an exclamation point or a caret (`!' or `^') follows the left bracket, then the range of characters within the
brackets is complemented (that is, anything except the characters inside the brackets is considered a match). To specify a
verbatim left bracket, the three-character sequence ``[[]'' has to be used.
(Be sure to quote any character that might otherwise be interpreted or modified by the operating system, particularly under Unix and
VMS.) If no matches are found, the specification is assumed to be a literal filename; and if that also fails, the suffix .zip is ap‐
pended. Note that self-extracting ZIP files are supported, as with any other ZIP archive; just specify the .exe suffix (if any) ex‐
plicitly. ```