Apa*_*ant 14 linux shell zip winzip
我正在尝试使用shell脚本命令压缩文件.我正在使用以下命令:
zip ./test/step1.zip $FILES
Run Code Online (Sandbox Code Playgroud)
其中$ FILES包含所有输入文件.但我收到的警告如下
zip warning: name not matched: myfile.dat
Run Code Online (Sandbox Code Playgroud)
还有一件事我发现文件夹中文件列表中最后的文件有上述警告,而且文件没有压缩.
任何人都可以解释我为什么会这样吗?我是shell脚本世界的新手.
Sto*_*ica 10
zip警告:名称不匹配:myfile.dat
这意味着该文件myfile.dat
不存在.
如果文件是指向不存在的文件的符号链接,则会出现相同的错误.
如你所说,无论是最后一个文件$FILES
,它都不会随警告一起添加到zip中.所以我认为你创造的方式有问题$FILES
.有可能在最后一个文件名的末尾有换行符,回车符,空格,制表符或其他不可见字符,从而导致某些内容不存在.试试这个例子:
for f in $FILES; do echo :$f:; done
Run Code Online (Sandbox Code Playgroud)
我打赌最后一行不正确,例如:
:myfile.dat :
Run Code Online (Sandbox Code Playgroud)
...或类似的东西,而不是:myfile.dat:
在最后一个之前没有字符:
UPDATE
如果你说脚本在运行之后dos2unix
就开始工作了,那就确认了每个人都怀疑的东西,不知何故,你的$FILES
列表末尾有一个回车符.
od -c
显示\ r \n回车.尝试echo $FILES | od -c
另一个可能导致zip warning: name not matched:
错误的原因是 的任何zip
环境变量设置不正确。
从手册页:
ENVIRONMENT
The following environment variables are read and used by zip as described.
ZIPOPT
contains default options that will be used when running zip. The contents of this environment variable will get added to the command line just after the zip command.
ZIP
[Not on RISC OS and VMS] see ZIPOPT
Zip$Options
[RISC OS] see ZIPOPT
Zip$Exts
[RISC OS] contains extensions separated by a : that will cause native filenames with one of the specified extensions to be added to the zip file with basename and extension swapped.
ZIP_OPTS
[VMS] see ZIPOPT
Run Code Online (Sandbox Code Playgroud)
就我而言,我zip
在脚本中使用并在环境变量中具有二进制位置,ZIP
以便我们可以zip
轻松更改为不同的二进制文件,而无需在脚本中进行大量更改。
例子:
ZIP=/usr/bin/zip
...
${ZIP} -r folder.zip folder
Run Code Online (Sandbox Code Playgroud)
然后将其处理为:
/usr/bin/zip /usr/bin/zip -r folder.zip folder
Run Code Online (Sandbox Code Playgroud)
并生成错误:
zip warning: name not matched: folder.zip
zip I/O error: Operation not permitted
zip error: Could not create output file (/usr/bin/zip.zip)
Run Code Online (Sandbox Code Playgroud)
第一个是因为它现在尝试添加folder.zip
到存档而不是将其用作存档。第二个和第三个是因为它试图将文件/usr/bin/zip.zip
用作(幸运的是)普通用户不可写的存档。
注意:这是一个非常古老的问题,但我没有在任何地方找到这个答案,所以我发布它以帮助未来的搜索者(包括我未来的自己)。