Telling Bourne shell from Korn shell at start-up

Telling Bourne shell from Korn shell at start-up

Post by Ronan Melennec STNA 7C p8240 BD » Wed, 17 Nov 1993 02:39:09



Hello,

I would like to find a way to tell Bourne shell from Korn shell
at start-up (when reading `$HOME/.profile').

A good solution shouldn't depend on the setting of
environment variable(s), as they are easily faked.

I have yet to find a suitable test, to be put in `$HOME/.profile',
to (for instance) bind keyboard keys when read by the Korn shell,
and to do nothing when read by the Bourne shell. I have read the FAQ,
to no avail. I have read the manuals too...

E-mail answers prefered.

Best regards,
--
Ronan Melennec        | Service Technique de la Navigation Aerienne
+33 1 60 79 82 40     | Departement 7

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by Chet Ram » Wed, 17 Nov 1993 09:43:56


Quote:>I would like to find a way to tell Bourne shell from Korn shell
>at start-up (when reading `$HOME/.profile').

if [ "$RANDOM" = "$RANDOM" ]; then
        sh
else
        ksh
fi

There are others, I'm sure.
--
"You can watch an actor absolutely sabotage a good script and then read
 reviews like 'Unfortunately, even the impressive talents of Cheech Marin
 could not salvage Anton Chekhov's trite and meandering script.'"


 
 
 

Telling Bourne shell from Korn shell at start-up

Post by damer.. » Wed, 17 Nov 1993 09:40:01



: Hello,

: I would like to find a way to tell Bourne shell from Korn shell
: at start-up (when reading `$HOME/.profile').

: A good solution shouldn't depend on the setting of
: environment variable(s), as they are easily faked.

: I have yet to find a suitable test, to be put in `$HOME/.profile',
: to (for instance) bind keyboard keys when read by the Korn shell,
: and to do nothing when read by the Bourne shell. I have read the FAQ,
: to no avail. I have read the manuals too...

: E-mail answers prefered.

: Best regards,
: --
: Ronan Melennec        | Service Technique de la Navigation Aerienne
: +33 1 60 79 82 40     | Departement 7

Mail bounced.  You might rely on the fact that the Bourne shell does
not have an alias command:

case `type alias` in
    *builtin*) #ksh;;
            *) #sh;;
esac

--
Dan Mercer                                            Applications + Plus

======================================================================
About a year ago I told the following joke which got a strong positive
reaction from only 1 in 5 (all males my age - 40+) and shrugs from the
rest.  I wonder if it is more relevant now?

"What does Mogadishu mean in Somali?"  Answer - "Saigon"

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by S. Lane Pier » Wed, 17 Nov 1993 13:26:11



Quote:>Hello,

>I would like to find a way to tell Bourne shell from Korn shell
>at start-up (when reading `$HOME/.profile').

>A good solution shouldn't depend on the setting of
>environment variable(s), as they are easily faked.

Would checking the output of ps do it for you?
 
 
 

Telling Bourne shell from Korn shell at start-up

Post by Ken Ke » Wed, 17 Nov 1993 19:12:31



Quote:>I would like to find a way to tell Bourne shell from Korn shell
>at start-up (when reading `$HOME/.profile').
>A good solution shouldn't depend on the setting of
>environment variable(s), as they are easily faked.
>I have yet to find a suitable test, to be put in `$HOME/.profile',
>to (for instance) bind keyboard keys when read by the Korn shell,
>and to do nothing when read by the Bourne shell. I have read the FAQ,
>to no avail. I have read the manuals too...

Environment variables faked by whom?  Since you said ``$HOME/.profile''
and not ``/etc/profile'', I assume you're doing this for yourself, not
for other users.  And even if it were for other users, so what?
There's no security problem, since all you want to do is run some
shell-dependant configuration commands; faking a variable wouldn't
do anybody any good.

Some of the other answers I've seen posted can detect that the shell is
not bourne shell, but they can't tell the difference between ksh and
other shells (e.g., bash, zsh).  This may not be an issue for you, but
I thought I should point it out.

In my own .profile, I use `basename ${SHELL-sh}` to decide which
shell-dependant code to run.

--
Ken "Hawkeye" Keys                  |  If you use TinyFugue, help support it.

------------------------------------+-------------+        1820 Cottonwood Av.
Official TF ftp site: glia.biostr.washington.edu  |        Carlsbad CA 92009

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by B.N.Blackmo » Wed, 17 Nov 1993 20:15:55





: >Hello,
: >
: >I would like to find a way to tell Bourne shell from Korn shell
: >at start-up (when reading `$HOME/.profile').
: >
: >A good solution shouldn't depend on the setting of
: >environment variable(s), as they are easily faked.
: >

: Would checking the output of ps do it for you?

NO... this is best avoided, ps is about the highest system load
causing command that there is ! People think that since it is a
standard command and thus it is ok to use, it is not. Whenever
you do a ps the system locks the machines memory and scans it,
this means that ps is about the only process that can run at
this time.

--
Brian Blackmore, The University of Kent at Canterbury, United Kingdom.
Beyond the shadow of a dream, who knows what lies.

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by James J. Yort » Wed, 17 Nov 1993 23:39:29



:
: Hello,
:
: I would like to find a way to tell Bourne shell from Korn shell
: at start-up (when reading `$HOME/.profile').
:
: A good solution shouldn't depend on the setting of
: environment variable(s), as they are easily faked.
:
: I have yet to find a suitable test, to be put in `$HOME/.profile',
: to (for instance) bind keyboard keys when read by the Korn shell,
: and to do nothing when read by the Bourne shell. I have read the FAQ,
: to no avail. I have read the manuals too...
:
: E-mail answers prefered.
:
: Best regards,
: --
: Ronan Melennec

Try checking the $0 parameter.  Example:

        case $0 in
                -ksh | ksh) < ksh stuff >
                                ;;

                -sh | sh )  < sh stuff >
                                ;;
        esac

That's a rough example.  Remember to watch for a possible leading '-' sign,
indicating that the shell is a login shell.  Some systems have
a leading /bin or whatever.  I'll let you embelish the case statement
to deal with that.  Be careful if you use * for pattern matching.  :-)

------------------------------+------------------------------------
Jim Yorton                    | Telephone:  +1 708-632-6695
Motorola Inc.                 |

Cellular Infrastructure Group | UUCP:       ...!uunet!motcid!yorton
Arlington Heights, IL   USA   |

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by Mitchell N. Perilste » Thu, 18 Nov 1993 01:03:46


Here's a repost of an interesting fragment: tells several shells apart.



Subject: Re: shell identification from within the shell
Date: 17 Jan 93 18:50:08 GMT
Organization: SINTEF DELAB, Trondheim, Norway.

Here is something I've put together.  I covers the most popular
shell variants in the Bourne/Korn genre.  Either invoke by
"eval $getshelltype", or cut the first and the last line....
The only reason not to make it a shell function is that the archaic
V7 shell doesn't implement functions.

  ~~h

getshelltype='
  shelltype=;
  isksh=yes;
  case "$SHELLVERS $SHELL_VERSION $KSH_VERSION $VERSION" in
    *PD\ KSH*)
        shelltype=pdksh;;
    *KSH* | *ksh*)
        shelltype=ksh;;
    zsh*)
        shelltype=zsh;;
    ash*)
        shelltype=ash
        isksh=no;;
  esac;
  case $shelltype in
    "") case $BASH_VERSION in
          ?*)   shelltype=bash;;
          "") case $RANDOM in
                  $RANDOM)
                        shelltype=`set 3 2 1; shift 2;
                                case $1 in
                                  2) echo v7sh;;
                                  1) echo sh;;
                                esac`;
                        isksh=no;;
                  *)    shelltype=ksh;;
                esac;;
        esac;;
  esac
'

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by Dik T. Wint » Thu, 18 Nov 1993 09:30:46


 > In my own .profile, I use `basename ${SHELL-sh}` to decide which
 > shell-dependant code to run.
 >
And I have the following in my .profile:
  case $SHELL in
  *ksh)
          # we have the k-shell, we think!
          if (set -o emacs) 2>/dev/null
...
The reason is that we had a workstation on loan that came standard with
ksh.  But ksh was non-functional.  In order to allow everybody with a
NIS entry with ksh as shell to log on the system, ksh was made a link
to sh!  In that situation SHELL lied.  I have had more situations where
I had to lie about the shell.
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by W M Brelsfo » Thu, 18 Nov 1993 23:48:34



Quote:

> I would like to find a way to tell Bourne shell from Korn shell
> at start-up (when reading `$HOME/.profile').

For years I've used the test Dave Korn suggested:

        one=1; test one -eq 1 && sh=ksh || sh=bsh

Bill Brelsford

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by James J. Yort » Thu, 18 Nov 1993 23:42:14



:

:  > In my own .profile, I use `basename ${SHELL-sh}` to decide which
:  > shell-dependant code to run.
:  >
: And I have the following in my .profile:
:   case $SHELL in
:   *ksh)
:           # we have the k-shell, we think!
:           if (set -o emacs) 2>/dev/null
: ...
: The reason is that we had a workstation on loan that came standard with
: ksh.  But ksh was non-functional.  In order to allow everybody with a
: NIS entry with ksh as shell to log on the system, ksh was made a link
: to sh!  In that situation SHELL lied.  I have had more situations where
: I had to lie about the shell.
: --
: dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland

IMHO, checking $SHELL may not be the best way.  According to my login(1)
man page (Sun OS 4.1.3), login(1) will set $SHELL for you.  But
if a user sets (resets) $SHELL in their own .profile *prior* to performing
the check you give above, it will yield the wrong result if their login
shell happens to be a non-ksh shell.

I would recommend keeping your statement above, but replacing $SHELL
with $0.  I don't believe $0 can ever lie.  :-)

------------------------------+------------------------------------
Jim Yorton                    | Telephone:  +1 708-632-6695
Motorola Inc.                 |

Cellular Infrastructure Group | UUCP:       ...!uunet!motcid!yorton
Arlington Heights, IL   USA   |

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by Ken Ke » Fri, 19 Nov 1993 03:13:38



> In my own .profile, I use `basename ${SHELL-sh}` to decide which
> shell-dependant code to run.

As I said in subsequent email, and as others have mentioned later, $0
is better than $SHELL.  But there still is one (unlikely) case in which
$0 can be wrong:  in ksh, when the .profile is sourced with the command
". .profile", the $0 will contain ".profile" instead of the name of the
shell.  This is not true for sh, bash, and zsh.  This isn't a problem
for logins, but you still may want to look out for it.

--
Ken "Hawkeye" Keys                  |  If you use TinyFugue, help support it.

------------------------------------+-------------+        1820 Cottonwood Av.
Official TF ftp site: glia.biostr.washington.edu  |        Carlsbad CA 92009

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by DaviD W. Sanders » Fri, 19 Nov 1993 03:28:25



Quote:

> I would like to find a way to tell Bourne shell from Korn shell
> at start-up (when reading `$HOME/.profile').

The method I use is

        case ~ in
        '~')    sh=bsh
                ;;
        *)      sh=ksh
                ;;
        esac


Space Science and Engineering Center    University of Wisconsin-Madison
"The Noah Webster of smileys"    - The Wall Street Journal, 15 Sep 1992

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by Ed Haymo » Fri, 19 Nov 1993 06:46:31



| > In my own .profile, I use `basename ${SHELL-sh}` to decide which
| > shell-dependant code to run.

| As I said in subsequent email, and as others have mentioned later, $0
| is better than $SHELL.  But there still is one (unlikely) case in which

Along these lines...  I have a shell script in which I want to define a
shell function if the user is running bash, but not if the user is
running ksh.  I can tell which the user is running, and enclose the bash
shell function in an if statement -- but ksh still
complains about a syntax error on the line in which the bash shell
function is defined.

How can I get ksh to ignore this line?

--
Ed Haymore   |   Amateur Radio: AA6EJ

 
 
 

Telling Bourne shell from Korn shell at start-up

Post by Ken Ke » Sat, 20 Nov 1993 03:23:09



Quote:>Along these lines...  I have a shell script in which I want to define a
>shell function if the user is running bash, but not if the user is
>running ksh.  I can tell which the user is running, and enclose the bash
>shell function in an if statement -- but ksh still
>complains about a syntax error on the line in which the bash shell
>function is defined.
>How can I get ksh to ignore this line?

You could put the command in a string, and eval it:

  case `basename $0` in
      bash)  eval 'funky command that ksh barfs on'
             ;;
      ksh)
             ;;
  esac

--
Ken "Hawkeye" Keys                  |  If you use TinyFugue, help support it.

------------------------------------+-------------+        1820 Cottonwood Av.
Official TF ftp site: glia.biostr.washington.edu  |        Carlsbad CA 92009

 
 
 

1. Trying to run nohup from Korn shell but getting Bourne shell errors

Help! Duh!
I would like to run a process in background with nohup on a
SOLARIS 2.3. How can I get the korn shell script detectsetup
to be interpreted under ksh.

In Korn shell I attempted...

$ /bin/ksh -c "/usr/bin/nohup /export/home/setupin/detectsetup&"

$ [1]   15276

$ Sending output to nohup.out

Checking the process I find....

USER   PID    PPID COMMAND
pagein 15414     1 sh /export/home/setupin/detectsetup
                            ^
                           | Why not Korn shell

My nohup.out file errors indicate Bourne shell is interpreting Korn shell
script (doesn't understand integer or [[).      

2. ipchains & firewall problems - HELP

3. Light Speed Bourne Shell! (was: Bourne shell tricks)

4. an ialloc()/iget() question

5. best korn shell resources and is there a korn shell faq

6. XEmacs & Netscape Problem

7. Bourne Shell compatible shells (was: Request: which shells)

8. SETUP: MUST MOUNT CDROM AT 0x01E8, IRQ 12

9. Korn/Bourne shell return code precision

10. How to pipe stderr to a command in Bourne or Korn shell

11. BOURNE/KORN Shell Compiler

12. bourne and korn shell incompatibilities

13. Looking for simple Bourne/Korn shell FIFO queue mechanism :-|