如何禁用“zip” bash 中发出警告?

发布于 2024-12-09 02:30:07 字数 339 浏览 0 评论 0原文

我想使用 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

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

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 zip in right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨夜星沙 2024-12-16 02:30:07

我想你想要安静的旗帜。

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

从手册页:

<前><代码>-q
- 安静的
静音模式;消除信息性消息和评论
提示。 (例如,在 shell 脚本和后台中很有用
任务)。

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).
羁客 2024-12-16 02:30:07

也许你可以尝试“add”而不是 update(-u) ?

来自手册页:

<前><代码>添加
更新现有条目并添加新文件。如果存档不存在
创建它。这是默认模式。

更新(-u)
如果文件系统上较新,则更新现有条目并添加新文件。
如果存档不存在,则会发出警告,然后创建一个新存档。

清新 (-f)
如果文件系统上较新,则更新存档的现有条目。做
不将新文件添加到存档中。

删除 (-d)
选择现有存档中的条目并将其删除。

maybe you can try "add" instead of 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.
霞映澄塘 2024-12-16 02:30:07

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

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

请持续率性 2024-12-16 02:30:07

您还可以删除不必要的输出行并保留正常状态信息可见,但您首先需要将 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.

基本上你的命令将如下所示:

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

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.

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.*'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文