Time output redirection

风格不统一 提交于 2020-01-06 14:41:04

问题


i have made an script to compress 3 files in zip and tar and measure the time to make a comparison, but when i try to redirect the output of time to a file the output stored in it is the tar and zip ones, could someone help me?

#!/bin/bash
if [ "$#" != 3 ]
  then
  echo "usage $0 file1 file2 file3"
  exit
fi

echo "Start\n"
echo "First zip:"  > time.txt
time >> time.txt zip -r $1.zip $1
echo "first zip done"
echo "first tar:" >> time.txt
time >> time.txt tar czf $1.tar.gz $1
echo "first tar done"
echo "second zip:" >> time.txt
time >> time.txt zip -r $2.zip $2
echo "Second zip done"
echo "second tar:" >> time.txt
time >> time.txt tar cfz $2.tar.gz $2
echo "Second tar done"
echo "third zip:" >> time.txt
time >> time.txt zip -r $3.zip $3
echo "third zip done"
echo "third tar:" >> time.txt
time >> time.txt tar cfz $3.tar.gz $3
echo "third tar done"
rm *.zip *.tar.gz
exit

Thanks


回答1:


Try this:

First create a blank time.txt file (at top of your shell script)

>time.txt

then, for each zip command, do something like this (NOTE: ';' is important before '}' :

{ time zip -r $1.zip $1 > /dev/null 2>&1; } 2>>time.txt


来源:https://stackoverflow.com/questions/27434522/time-output-redirection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!