We help IT Professionals succeed at work.

Hoist the colors! We’ve added location flags to usernames sitewide, so it's easier to connect with the global community on EE. View My Profile

x

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

1,100 Views
Last Modified: 2013-12-26
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

Harisha M GEngineer
CERTIFIED EXPERT

Commented:
Put this line as the first line in script:

#!/usr/bin/ksh

Open in new window

Harisha M GEngineer
CERTIFIED EXPERT

Commented:
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.

 
CERTIFIED EXPERT
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

CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.