How to force user to place 'ksh' before running a shell script

tindavid
tindavid used Ask the Experts™
on
Here is the issue, I have written a ksh script that is to be run both Unix and Linux, since the script containts logic and features that only "korn" will give better/valid result, so what I want is:

whenever this program is excuted, make sure it will be running like below:

$ ksh myprog.sh

if user enter $ sh myprogram.sh
the program will prompt the use to enter "ksh myprogram.sh" instead.

Why I am asking for this,  so I don't need to care wheather the default shell for a give user is a bash/csh/

What I want is logic in "myprogram.sh" to check if user does enetr the ksh before the $0 !

Thank you,

David


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Put this line as the first line in script:

#!/usr/bin/ksh

Open in new window

Or, if the shell is located in /bin:

#!/bin/ksh

Open in new window

Author

Commented:
Noop,  in Linux,  this #!/bin/ksh do not take effect. it will still be executed under the deault shell (such as bash), of course, if the ksh has been set to the default shell, it will work, but the problem is I have no idea if the default shell for a given user is 'ksh' of bash'

Please double check this phononmenum,   all I want is:

$ ksh abcd.sh
working ...
hello world
$ sh abcd.sh
sh abcd.sh is not allowed, you must enter ksh abcd.sh ... exit now.

 
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Top Expert 2007

Commented:
This is a tricky one.

If you invoke wth csh/tcsh, it makes it much more difficult.

Perhaps the following will be fine.
#!/bin/ksh
ksh=$(set | grep  ksh)

if [ -z "$ksh" ]
then
   echo "$0 needs to run under ksh"
   echo "Type $0 or ksh $0"
   exit 1
fi

Open in new window

Author

Commented:
Hi Tintin,

Almost there, It works ok in Linux, but not in Unix,

Solaris
xterm04[ora9i][/sw/users/oracle/dtin] >cat x.sh
#!/usr/bin/ksh
ksh=$(set | grep  ksh)

xterm04[ora9i][/sw/users/dtin] >sh x.sh
x.sh: syntax error at line 3: `ksh=$' unexpected
xterm04[ora9i][/sw/users/dtin] >

Thank you,

David

Top Expert 2007
Commented:
Change

ksh=$(set | grep  ksh)


to

ksh=`set | grep  ksh`

Author

Commented:
Hi TinTin,

Thanks for the suggestion.

ksh=`set | grep  ksh | grep -v SHELL`

I found that this command could return following if the default shell is already a korn shell:

xterm04[ora9i][/sw/users/oracle] >cat /etc/passwd| grep oracle
oracle:x:1003:101::/sw/users/oracle:/bin/ksh
xterm04[ora9i][/sw/users/oracle] >set | grep ksh
SHELL=/bin/ksh

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial