next up previous
Next: 在 shell 中要用怎樣的 pattern 來表示除了 "." 與 ".." Up: 初學者可能會問的基本問題 Previous: 我要如何將 csh 的 stdout 與 stderr 導向到不同的地方呢?

我如何在 .cshrc 中判斷是否在 login shell 中?

當有人這麼問的時候,通常要問的是

要如何判斷是否是一個 interactive shell? 或是問要如何判斷是否是 最上層的 shell ?

若你是要問 "是否在 login shell 中"(註:就是在做完 .cshrc 後,會 再去做 .login),那麼你也許用 "ps" 和 "$$" 隨便弄一弄,就能知道了 。因為通常 login shells 的名字在 "ps" 看起來都是由 '-' 做開頭的。 如果你是真的對另外兩個問題感興趣,那麼這裡有個方法可以讓你在  .cshrc 中判斷。

		if (! $?CSHLEVEL) then
		#
		# This is a "top-level" shell,
		# perhaps a login shell, perhaps a shell started up by
		# 'rsh machine some-command'
		# This is where we should set PATH and anything else we
		# want to apply to every one of our shells.
		#
		    setenv CSHLEVEL 0
		    set home = ~username   # just to be sure
		    source ~/.env          # environment stuff we always want
		else
		#
		# This shell is a child of one of our other shells so
		# we don't need to set all the environment variables again.
		#
		    set tmp = $CSHLEVEL
		    @ tmp++
		    setenv      CSHLEVEL        $tmp
		endif

		# Exit from .cshrc if not interactive, e.g. under rsh
		if (! $?prompt) exit

		# Here we could set the prompt or aliases that would be useful
		# for interactive shells only.

		source ~/.aliases



Tan Koan-Sin
1999-03-02