how to set the shell to bash if there is bash otherwise set to ksh

TIMFOX123
TIMFOX123 used Ask the Experts™
on
I am in linux , aix , solaris

Usually there is bash on the box, sometimes not
if there is bash on the box, I want to run my scripts in bash.
if there is not bash on the box THERE WILL BE KSH and i would like to set my scripts to run
in ksh ( so it does not default to sh )

how can I do this ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bradley FoxLAN/WAN Systems Administrator

Commented:
You can set your default shell per user in /etc/passwd.  Just change the last option on the line from /bin/sh to /bin/bash or /bin/ksh

This link explains the /etc/passwd layout
http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/
Most Valuable Expert 2013
Top Expert 2013

Commented:
Omit the shebang line #!...

and call your script like

[[ -x /bin/bash ]] && INTP=/bin/bash || INTP=/bin/ksh; $INTP /path/to/script

If your default login shell is either bash or ksh then you can do

$SHELL /path/to/script

Here too you must omit #!...
Most Valuable Expert 2013
Top Expert 2013

Commented:
This is another trick.

Prefix your script (again without the shebang) with the following code (I took the first part from your other question):

function shellselect {
if [[ -e "/bin/bash" ]];  then
                INTERP="/bin/bash"
                else
                                INTERP="/bin/ksh"
fi; }

if [[ $1 != "12345selected54321" ]];  then
   shellselect
   $INTERP $0 "12345selected54321" "$@"
   exit
   else shift
fi
# actual code starts here, next two lines just for demonstration ...
echo $BASH # line will be empty if ksh is used, else it will contain the path to bash.
echo $@ # to show that the params are still there
# ....
# ....
Top Expert 2007
Commented:
Do the servers that have bash also have ksh?

If so, then just add

#!/bin/ksh

to your scripts

Author

Commented:
Thank you so much everybody but Tintin just hit this one 100%

thx Tintin

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