是否可以在bash中重新定義文件描述符(例如stderr
)?如何在bash中重新定義stdout/stderr?
我想默認發送所有輸出到文件,同時仍然能夠使用原始stderr
和stdout
。
#!/bin/bash
echo "Error: foo bar" 1>2
REAL_STDERR=2
REAL_STDOUT=1
2=open("/tmp/stderr.log")
1=open("/tmp/stdout.log")
echo "This goes to stdout.log"
if ! curl doesntexist.yet; then
echo "Error: Unable to reach host. See stderr.log for details" 1>REAL_STDERR
fi
由於旁邊 - 全部大寫的名字是爲操作系統或shell本身具有含義的變量指定的,而小寫名稱是爲應用程序使用而保留的。請參閱http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html,請記住,環境變量和shell變量共享一個名稱空間(分配給與現有環境變量名稱重疊的shell變量將覆蓋後者),所以命名約定必然適用於這兩者。 –
謝謝,我不知道。 –