我在.zshrc文件下面的函數(在OS X上,使用的coreutils日期執行):
# Display timestamped debugging messages when ZSHDBG is enabled.
function zshdbg() {
if [[ $ZSHDBG -eq 1 ]]; then
purple="\x1b[35;01m"
reset="\x1b[39;49;00m"
now=$(date '+%F %T')
nanoseconds=$(date +%N)
# For some reason on OSX, %N is sometimes returned as "N" rather than the
# current nanosecond timestamp. This is a nasty hack for providing a
# default value in those unfortunate cases.
# if [[ "$nanoseconds" -eq 'N' ]]; then
# nanoseconds='000000000'
# fi
echo "$purple$now [$nanoseconds] $1$reset"
fi
}
出於某種原因,在納秒有時返回爲 「N」:
2014-10-28 21:45:42 [N] Antigen installation detected.
2014-10-28 21:45:42 [N] Last update on . Current date 2014-10-28 21:45:42. It's been days since the last update.
2014-10-28 21:45:42 [N] Loading antigen.
2014-10-28 21:45:42 [N] Setting oh-my-zsh as the default antigen plugin repository.
2014-10-28 21:45:42 [N] Enabling OS X specific plugins.
2014-10-28 21:45:42 [N] Loading plugin zsh-users/zsh-syntax-highlighting.
2014-10-28 21:45:42 [N] Loading theme jonathan.
2014-10-28 21:45:42 [N] Applying the antigen configuration.
2014-10-28 21:45:42 [N] Setting default exports.
2014-10-28 21:45:43 [N] OS X detected, executing OS specific configuration.
2014-10-28 21:45:43 [N] Homebrew detected. Configuring paths.
2014-10-28 21:45:43 [084632000] Finished loading .zshrc.
這是使用的coreutils date命令:
which date
/usr/local/opt/coreutils/libexec/gnubin/date
但是,如果我顯式調用/ bin中/日,我仍然得到類似的行爲:
2014-10-28 21:50:47 [N] Antigen installation detected.
2014-10-28 21:50:47 [N] Last update on . Current date 2014-10-28 21:50:47. It's been days since the last update.
2014-10-28 21:50:47 [N] Loading antigen.
2014-10-28 21:50:47 [N] Setting oh-my-zsh as the default antigen plugin repository.
2014-10-28 21:50:47 [N] Enabling OS X specific plugins.
2014-10-28 21:50:47 [N] Loading plugin zsh-users/zsh-syntax-highlighting.
2014-10-28 21:50:47 [N] Loading theme jonathan.
2014-10-28 21:50:47 [N] Applying the antigen configuration.
2014-10-28 21:50:47 [N] Setting default exports.
2014-10-28 21:50:47 [N] OS X detected, executing OS specific configuration.
2014-10-28 21:50:47 [N] Homebrew detected. Configuring paths.
2014-10-28 21:50:47 [N] Finished loading .zshrc.
我一直在挖掘date,zsh和homebrew的coreutils的文檔,但沒有找到任何相關的東西。任何想法,爲什麼這可能會發生?
我認爲這是路徑和格式的結合 - 兩個問題我實際上在同一時間修復,但沒有意識到另一個也是一個罪魁禍首。當我進行原始測試時,我正在檢查我們是否在OSX上,然後在* oh-my-zsh被抗原加載後預先加入coreutils *,但是在我的zsh配置之前調用該函數之前, coreutils/homebrew路徑是前綴。這段代碼被移動到我的.zshenv文件的開頭,這樣子路徑是正確的。似乎現在工作正常。 – nfarrar 2014-10-29 14:36:16
@nfarrar實際上,我會將格式恢復到以前的樣子 - 除極少數例外情況外,您應該在雙引號之間保留可變引用以防止意外的分詞和通配符擴展。在這種情況下,它可能是安全的,但看看[這個最近的問題和它的答案](http://stackoverflow.com/questions/26616849/how-to-set-shell-variable-to-result-of-evaluation -in-os-x)查看未引用變量引用的意外結果示例。 – 2014-10-29 15:24:16