如何在bash中禁用'zip'警告?

时间:2011-10-09 20:19:42

标签: linux bash shell ubuntu zip

我想使用bash shell压缩文件,所以我使用了:

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

4 个答案:

答案 0 :(得分:31)

我想你想要安静的旗帜。

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

从手册页:

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

答案 1 :(得分:2)

您可能不应该告诉zip更新档案(-u)。如果没有-u开关zip尝试将文件添加到存档,并且应该在没有警告的情况下创建不存在的存档。

答案 2 :(得分: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.

答案 3 :(得分: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.*'