{"id":1244,"date":"2017-03-13T22:16:26","date_gmt":"2017-03-13T22:16:26","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=1244"},"modified":"2017-03-13T22:16:26","modified_gmt":"2017-03-13T22:16:26","slug":"a-plethora-of-shells-what-is-my-shell-interpreter","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/03\/13\/a-plethora-of-shells-what-is-my-shell-interpreter\/","title":{"rendered":"a plethora of shells &#8211; what is my shell interpreter"},"content":{"rendered":"<p>I kept seeing odd error messages from my shell script that simply made no sense.\u00a0 No matter what changes I made a new error seemed to pop up.\u00a0 A short while later I did notice at the top of the script that it was using a korn shell interpreter.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/usr\/bin\/ksh\r\n<\/code><\/pre>\n<\/div>\n<p>This was not as helpful as you might initially guess.\u00a0 Sure there are differences between the two shells but most of the scripts that I write would work under either.\u00a0 Besides, I had a small script with the new code I wanted to test and it worked fine under both the bash and korn shell.<\/p>\n<p>I didn&#8217;t initially consider that the the script itself wasn&#8217;t not being run in the same manner by the calling system as by me.<\/p>\n<p>In the end I wanted to determine if the shell was the same as what I had selected &#8211; after all how could it not be my shell.<\/p>\n<p>The most straight forward method would be to see which shell is interpreting.\u00a0 It is just as easy as checking which process or processes are running.\u00a0 Just to see which programs, in this case our shell, is running I ran the process status command (ps).<\/p>\n<p>Nothing else is really running in my Korn shell example.\u00a0 The only two processes is the korn shell and the ps command.\u00a0 The Korn shell is not really all that surprising as this is a terminal window for my user and the Korn shell was the shell setup for in the \/etc\/passwd file.<\/p>\n<p>Korn shell<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; ps -f\r\n\r\n     UID   PID  PPID   C    STIME TTY         TIME CMD\r\n   fkadm  3819  3816   0 14:52:41 pts\/2       0:00 -ksh\r\n   fkadm  4609  3819   0 14:54:37 pts\/2       0:00 ps -f<\/code><\/pre>\n<\/div>\n<p>It is almost the exact same story for the Bash shell.\u00a0 My user was started with a Korn shell and (process 23509) and then ran the Bash shell (process 23541).\u00a0 The ps command is then run from the Bash shell reporting on anything that is running.<\/p>\n<p>Bash shell<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; ps -f\r\n\r\n     UID   PID  PPID   C    STIME TTY         TIME CMD\r\n   fkbin 23509 23507   0 10:27:31 pts\/4       0:00 -ksh\r\n   fkbin 23541 23509   0 10:27:31 pts\/4       0:00 bash\r\n   fkbin  4128 23541   0 14:53:51 pts\/4       0:00 ps -f<\/code><\/pre>\n<\/div>\n<p>The starting point for my finding my shell is to know which of the many processes to look at.\u00a0 The good news is that I am able to get my process id using the special shell variable &#8220;$$&#8221;.<\/p>\n<p>Everything from that point is pretty straight forward.\u00a0 We can use the -p and -o arguments with the ps command to see specific values for a specific process.<\/p>\n<p>&nbsp;<\/p>\n<p>process status (ps)<\/p>\n<table class=\"w3-table-all\">\n<tbody>\n<tr>\n<th>argument<\/th>\n<th>Description<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>-p<\/td>\n<td>process id<\/td>\n<td>selects process status from list where the process id matches this value.<\/td>\n<\/tr>\n<tr>\n<td>-o<\/td>\n<td>format<\/td>\n<td>The format is one of many possible fields that can be selected from the process status.\u00a0 There are quite a few possibilities, but the ones that stood out as the most immediately interesting are as follows.<\/p>\n<ul>\n<li>user<\/li>\n<li>pid<\/li>\n<li>ppid<\/li>\n<li>stime<\/li>\n<li>tty<\/li>\n<li>args<\/li>\n<li>comm<\/li>\n<\/ul>\n<p>The first six should look familiar as these are the values that are output when doing a full listing (-f).\u00a0 The comm format is the actual command which in this case is the interpreter while the args is both the command and its arguments.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>With this little bit of knowledge I had enough information to query what interpreter was running my script.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/bash\r\nMYPID=$$\r\nMYSHELL=`ps -p $MYPID -o comm | egrep -v &quot;CMD|COMMAND&quot;`\r\nMYCOMMAND=`ps -p $MYPID -o args | egrep -v &quot;CMD|COMMAND&quot;`\r\necho the shell for process id $MYPID is $MYSHELL.  \r\necho the entire command is $MYCOMMAND\r\n<\/pre>\n<p>This type of shell goodness should be all that it takes to find out more about how you are being called.\u00a0 I was a bit surprised to see that my script was being called with a different shell altogether.<\/p>\n<p>The shell calling my program was \/usr\/bin\/sh which is a lot less compatible with either the Korn or Bash shell.\u00a0 A bit more digging and I found out that the script was being passed directly to the shell interpreter.\u00a0 This explains why the Korn shell wasn&#8217;t being used.<\/p>\n<div class=\"sbody-code\">\n<pre><code>sh \/home\/specialuser\/scripts\/myscript.sh<\/code><\/pre>\n<\/div>\n<p>Well, I guess this is proof that you cannot always trust IT.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I kept seeing odd error messages from my shell script that simply made no sense.\u00a0 No matter what changes I made a new error seemed to pop up.\u00a0 A short while later I did notice at the top of the &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/03\/13\/a-plethora-of-shells-what-is-my-shell-interpreter\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1244"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=1244"}],"version-history":[{"count":6,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1244\/revisions"}],"predecessor-version":[{"id":2191,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1244\/revisions\/2191"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=1244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=1244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=1244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}