2011-10-09 54 views
7

我想用的bash shell来压缩文件,所以我用:如何在bash中禁用“zip”警告?

echo -n 'Insert the file path:' 
read path 
echo 'Hello World' > ${path} 
zip -u ${path}.zip ${path} 

当我运行该脚本,它给了我一个警告:

zip warning: test.zip not found or empty 
adding: test (deflated 66%) 

它工作得很好,但我怎么能禁用此警告?我是否正确使用zip

回答

18

我想你想要安静的国旗。

zip -uq ${path}.zip ${path} 

从手册页:

-q 
--quiet 
      Quiet mode; eliminate informational messages and comment 
      prompts. (Useful, for example, in shell scripts and background 
      tasks). 
+0

+1这就是我要找的。谢谢 :) –

1

也许你可以试试 “添加”,而不是更新(-u)?

从手册页:

add 
      Update existing entries and add new files. If the archive does not exist 
      create it. This is the default mode. 

    update (-u) 
      Update existing entries if newer on the file system and add new files. 
      If the archive does not exist issue warning then create a new archive. 

    freshen (-f) 
      Update existing entries of an archive if newer on the file system. Does 
      not add new files to the archive. 

    delete (-d) 
      Select entries in an existing archive and delete them. 
1

也许你不应该告诉zip更新存档(-u)。如果没有-u开关zip会尝试将文件添加到存档,并应创建不存在警告的不存在的存档。

0

您也可以删除不必要的输出行并保留正常状态信息可见,但您首先需要将stderr重定向到stdout。例如下面的命令将从许多zip文件中只提取一些特定的文件,但不会显示emnpty行,也不会抱怨找不到文件。这样,你就仍然有一些输出日志,调试等

unzip -jn archivedlogfiles\* \*logfile\* 2>&1 | grep -vE '^$|^caution.*' 
Archive: archivedlogfiles1.zip 
    inflating: little_logfile_20160515.log 
    inflating: little_logfile_20160530.log 
Archive: archivedlogfiles2.zip 
Archive: archivedlogfiles3.zip 
Archive: archivedlogfiles4.zip 
Archive: archivedlogfiles5.zip 
Archive: archivedlogfiles6.zip 
Archive: archivedlogfiles7.zip 
Archive: archivedlogfiles8.zip 
    inflating: little_logfile_20160615.log 
    inflating: little_logfile_20160630.log 
Archive: archivedlogfiles9.zip 
2 archives were successfully processed. 
7 archives had fatal errors. 

基本上你的命令将则是这样的:

zip -u ${path}.zip ${path} 2>&1 | grep vE '^zip\swarning.*'