Time output redirection

2019-09-17 21:49发布

问题:

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