2011-10-09 98 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.*'