Linux 如何在 bash 中禁用“zip”警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7706311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:34:43  来源:igfitidea点击:

How to disable 'zip' warning in bash?

linuxbashshellubuntuzip

提问by Eng.Fouad

I want to zip a file using bash shell, so I used:

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

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

When I run this script, it gives me a warning:

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

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

It works just fine but how can I disable this warning? Am I using zipin right way?

它工作得很好,但如何禁用此警告?我使用zip方法正确吗?

采纳答案by Gazler

I think you want the quiet flag.

我想你想要安静的旗帜。

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

From the man pages:

从手册页:

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

回答by Kent

maybe you can try "add" instead of update(-u) ?

也许您可以尝试“添加”而不是 update(-u) ?

from man page:

从手册页:

   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.
   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.

回答by sth

Probably you should not tell zipto update an archive (-u). Without the -uswitch ziptries to add files to an archive, and should create non-existing archives without warnings.

可能您不应该告诉zip更新存档 ( -u)。没有-u开关zip尝试将文件添加到存档,并且应该创建不存在的存档而不会发出警告。

回答by uldics

You can also remove unnecessary output lines and leave normal status info visible, but you first need to redirect the stderr to stdout. For example following command will extract from many zip files only some specific files, but will not show emnpty lines nor complain for not found files. In that way you still have some output for logging, debugging etc.

您还可以删除不必要的输出行并保留正常状态信息可见,但您首先需要将 stderr 重定向到 stdout。例如,以下命令将从许多 zip 文件中仅提取一些特定文件,但不会显示空行,也不会抱怨找不到文件。通过这种方式,您仍然有一些用于日志记录、调试等的输出。

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.

Basically your command would then look like this:

基本上你的命令看起来像这样:

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