2017-06-06 102 views
0

目前,我有一個劇本,我想所有的輸出重定向到一個既文件和控制檯。輸出重定向到文件和控制檯

#!/bin/bash 
touch /mnt/ybdata/ybvwconf.log 
{ 
    ... [Do some script code here] 
} 2>&1 | tee -a /mnt/ybdata/ybvwconf.log 

上面,你可以看到我目前的代碼,它工作得很好。它將所有輸出打印到控制檯,並將其輸出到ybvwconf.log文件。但是,我正在尋找一種消除大括號的方法。沿線的東西:

#!/bin/bash 
touch /mnt/ybdata/ybvwconf.log 
exec 2>&1 | tee -a /mnt/ybdata/ybvwconf.log 

... [Do some script code here] 

我試過這種方法,可悲的是它不工作。我沒有收到任何錯誤,但沒有內容出現在我的日誌文件中。任何想法可能是錯誤的?

+1

爲什麼要消除大括號? – 123

+0

你不需要'的touch'第一 – Attie

+0

可能的複製[重定向標準輸出複製到從bash腳本本身的日誌文件(https://stackoverflow.com/questions/3173131/redirect-copy-of-stdout-到日誌文件由內而外地-的bash腳本-本身) – DevSolar

回答

1

你可以在你的腳本的頂部放置這兩個輸出和錯誤重定向到文件,並將它們顯示終端爲好。

#!/bin/bash 
exec &> >(tee /mnt/ybdata/ybvwconf.log; exit) 

# your script code goes here 
+0

這似乎並不如預期,當我運行它的工作。它給出了一個錯誤:爲你的腳本 '沒有這樣的文件或目錄的/ dev/FD/62' – Kristianasp

+0

是'到/ mnt/ybdata /'有效的和可訪問的目錄?嘗試使用此方法測試:'EXEC&>>(TEE /tmp/ybvwconf.log;出口)' – anubhava

+0

是的它是。我也使用'/ tmp/ybvwconf.log'來嘗試,並且在腳本開始時出現同樣的錯誤。 – Kristianasp

相關問題