如何判断当前的shell我的工作? [英] How to determine the current shell I'm working on?

查看:111
本文介绍了如何判断当前的shell我的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能确定当前的shell我的工作?

How can I determine the current shell I am working on?

单独会在 PS 命令的输出是足够的?

Would the output of the ps command alone be sufficient?

如何能在这样的UNIX不同口味做些什么呢?

How can this be done in different flavors of UNIX?

推荐答案


  • 有三种途径寻找当前shell的可执行的名称

请注意,所有三种途径可如果壳的可执行文件是 / bin / sh的被愚弄,但它的确是一个改名庆典,例如(这经常发生)。

Please note that all 3 approaches can be fooled if the executable of the shell is /bin/sh but it's really a renamed bash, for example (which frequently happens).

因此,你是否 PS 输出将不与回答的第二个问题的不一定

Thus your second question of whether ps output will do is answered with "not always".


  1. 回声$ 1,0 - 将打印的程序名...这在外壳的情况下是实际的外壳

  1. echo $0 - will print the program name... which in the case of shell is the actual shell

PS -ef | grep的$$ | grep的-v grep的 - 将寻找当前进程ID在运行中的进程列表中。当前进程之中外壳,它包括外壳。

ps -ef | grep $$ | grep -v grep - will look for current process ID in the list of running processes. Current process being shell, it will include shell.

这是不是100%可靠,因为你可能有其他进程的 PS 房源包括相同数量的shell的进程ID,特别是如果该ID是一个小#(如:如果shell的PID为5,你可能会发现过程称为Java5的,或在同一的grep 输出的perl5!)。 其中presents第二个问题为PS的方式,在外壳上名称为并不总是可靠的顶部。

This is not 100% reliable as you might have OTHER processes whose ps listing includes the same number as shell's process ID, especially if that ID is a small # (e.g. if shell's PID is "5", you may find processes called "java5" or "perl5" in the same grep output!). Which presents the second problem to "ps" approach, on top of the shell name being not always reliable.

回声$ SHELL 当前shell的路径是在 SHELL 任何shell变量。最后一个需要注意的是,如果你明确地推出一个shell作为子(例如,它不是你的登录shell),你会得到你的登录shell的值,而不是 - 如果这是有可能的,使用 PS $ 1,0 方法。

echo $SHELL The path to the current shell is in SHELL variable for any shell. The caveat for the last one is that if you launch a shell explicitly as a subprocess (e.g. it's not your login shell) you will get you login shell's value instead - if that's a possibility, use the ps or $0 approach.

但是,如果可执行文件不匹配真实的外壳(例如 / bin / sh的实际上是bash或ksh)中,则需要启发。下面是一些环境变量具体到不同的炮弹:


  • However, if the executable is not matching real shell (e.g. /bin/sh is actually bash or ksh), you need heuristics. Here are some environmental variables specific to various shells:


    • $版本设置有关tcsh

    $ BASH 设置在庆典

    $壳(小写)设置为实际的外壳名csh或tcsh

    $shell (lowercase) is set to actual shell name in csh or tcsh

    $ ZSH_NAME 设置上的zsh

    KSH有 $ PS3 $ PS4 集,而正常的Bourne shell( SH )只有 $ PS1 $ PS2 集。这通常似乎是最难区分 - SH KSH 我们之间的整套伴有环境变量的唯一区别安装在Solaris上Boxen有是 $ ERRNO $ FCEDIT $ LINENO $ PPID $ PS3 $ PS4 $ RANDOM $秒。 $ TMOUT

    ksh has $PS3 and $PS4 set, whereas normal Bourne shell (sh) only has $PS1 and $PS2 set. This generally seems like the hardest to distinguish - the ONLY difference in entire set of envionmental variables between sh and ksh we have installed on Solaris boxen is $ERRNO, $FCEDIT, $LINENO, $PPID, $PS3, $PS4, $RANDOM, $SECONDS, $TMOUT.

    这篇关于如何判断当前的shell我的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆