The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140508-Delphi-2007--Project-Options--Cannot-Edit-Application-Title-HelpFile-Icon-Theming

    20140430-Fiddler-Filter-Actions-Button-Run-Filterset-now

    20140424-Windows-7-free-disk-space

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,749 other followers

Determining the current shell in *n*x variants including ESXi

Posted by jpluimers on 2017/02/08

On most systems, I use bash as shell, but not all systems have it, for instance the shell.xs4all.nl server uses tcsh and ESXi 4+ uses a very limited ash shell from busybox (ESX 4 had bash though).

There is this huge script that covers many shell and operating system versions (even DOS, Windows) and interpreters (python, ruby, php, etc) what shell is this which I got through Stéphane Chazelas‘s answer in linux – determine shell in script during runtime – Unix & Linux Stack Exchange

I wanted a shorter thing that works in current Linux, BSD, OS X and ESXi versions.

Some very short scripts are less reliable, for instance echo $SHELL looks nice, but isn’t always set.

Similar for echo $0 which will fail for instance if it shows as sh but in fact is a different shell in disguise.

This works for bash, tcsh and busybox sh, is a bit more precise than getting $0. It’s based on HOWTO: Detect bash from shell script – Stack Overflow:

lsof -p $$ | awk '(NR==2) {print $1}'

But on ESXi it shows this because lsof doesn’t take any parameter there and just dumps all information:

----------+---------------------+---------------------+--------+------------------

It’s because lsof on ESXi always shows this header where Cartel and World aren’t exactly well documented:

Cartel | World name | Type | fd | Description
----------+---------------------+---------------------+--------+------------------

Empirically for non VM related processes, it looks like the Cartel is the PID and World name the command.

On Linux and BSD based systems, the header looks like this, so command and PID are reversed in ESXi:

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

This command then works on both ESXi, OS X, Linux and BSD assuming you can word search for the PID and noting that PID/command will be reversed on ESXi as compared to OSX/Linux/BSD:

lsof -p $$ | grep -w $$ | awk '(NR==2) {print $1,$2}'

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: