輸出 time 命令的結果到檔案中

2021-06-23 06:21:39 字數 1491 閱讀 4978

譯至:

由於輸出 time 命令的結果到檔案時使用的錯誤的方式,所以將其記錄下來。

環境是bash。

將執行的a.out程式的輸出和其所花的時間重定向到日誌檔案中

time ./a.out > logfile

time ./a.out | tee logfile

上面的例子是重定向結果到logfile

,下面的例子是通過tee把標準輸出輸出到檔案。 

但是只有time的輸出沒有被記錄到檔案中。

這是因為time的結果是通過標準錯誤輸出的。

所以不把標準錯誤輸出重定向到檔案中是不行的。

把錯誤輸出也重定向到檔案。

time ./a.out >& logfile

time ./a.out > logfile 2>&1

time ./a.out 2>&1 | tee logfile

然而,這也是沒有用的。

./a.out >& logfile

./a.out > logfile 2>&1

./a.out 2>&1 | tee logfile

因為這只是意味著把a.out的錯誤和標準輸出重定向到logfile。

我們是想把 time ./a.out 的結果輸出到logfile,所以:

(time ./a.out) >& logfile

(time ./a.out) > logfile 2&>1

(time ./a.out) 2>&1 | tee logfile

用括號括起來。這樣就搞定了。

換句話說,你是在乙個子shell中執行。

另外最新的bash不僅支援》&也支援&>。

也可以用 指定一組命令。

>& logfile

> logfile 2&1

2>&1 | tee logfile

』 。後面的冒號;也不要忘了。

順便說一下,bash內建的命令time和gnu的time命令是不一樣的。通過追加完整的路徑/usr/bin/time 可以執行gnu命令。

但是和內建的bash不同的是,不加括號也可以將所有的輸出重定向到檔案。當然加括號也沒問題。

/usr/bin/time ./a.out >& logfile

/usr/bin/time ./a.out > logfile 2>&1

/usr/bin/time ./a.out 2>&1 | tee logfile

而且有輸出到檔案的選項-o。

/usr/bin/time -o logfile ./a.out
但是這只會把time的結果寫到檔案。使用追加選項-a就可以了。

/usr/bin/time -a -o logfile ./a.out > logfile
gnu的time命令跟bash的time比的話,資料更詳細,也能指定多種輸出格式。

詳細的情況請使用man time,bash的time的話使用help time。

mysql 輸出mysql查詢結果到檔案

mysql select count 1 from table into outfile tmp test.xls query ok,31 rows affected 0.00 sec 在目錄 tmp 下會產生檔案test.xls 遇到的問題 mysql select count 1 from ta...

CentOS 中命令視窗命令輸出結果亂碼

命令視窗命令輸出結果亂碼只要是原因是系統支援的字元編碼格式問題 檢視系統本地字元編碼格式 root localhost locale lang en us.utf 8 lc ctype en us.utf 8 lc numeric en us.utf 8 lc time en us.utf 8 lc...

stl 輸出unicode到檔案中

在vs2008中,如果專案設定了unicode字符集,把中文輸出到檔案中經常會遇到錯誤。在mfc專案中,可以使用以下語句來實現unicode到多位元組字元的轉換 uses conversion cstring strlog t 我愛大家 const char cplog const char w2a...