Link to home
Start Free TrialLog in
Avatar of tindavid
tindavid

asked on

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

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


Avatar of Harisha M G
Harisha M G
Flag of India image

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

Avatar of tindavid
tindavid

ASKER

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.

 
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

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

ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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