开发者

How to disable 'zip' warning in bash?

开发者 https://www.devze.com 2023-04-12 08:08 出处:网络
I want to zip a file using bash shell, so I used: echo -n \'Insert the file path:\' read path echo \'Hello World\' > ${path}

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开发者_JAVA技巧 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?


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


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.


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.


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.*'
0

精彩评论

暂无评论...
验证码 换一张
取 消