[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 編程技術 > Shell > 正文
如何在 .cshrc 中判斷是否在 login shell 中?
Steve Hayman (2001-04-19 10:36:13)
當有人這問的時候,通常要問的是 

要如何判斷是否是一個 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  (http://www.fanqiang.com)
    進入【UNIX論壇

相關文章

======
 

★  樊強制作 歡迎分享  ★