Operating System - HP-UX
1753971 Members
8576 Online
108811 Solutions
New Discussion юеВ

Re: how to change shell within the script

 
vz7r1x
Regular Advisor

how to change shell within the script

I have an script which starts with #!/bin/sh but my native shell is ksh. So in the script, I say echo $SHELL after /bin/shell line and shell is ksh.

I just want shell changed for this session only. How can I change my sheel from ksh to bsh within the script?

Thanks
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: how to change shell within the script

Hi:

> How can I change my sheel from ksh to bsh within the script?

You don't have to if your script begins with a "she-bang" interpreter line. In HP-uX:

#!/bin/sh

...is equivalent to:

#!/usr/bin/sh

...which is the POSIX shell --- the HP-UX standard. You login shell (as defined in '/etc/passwd') might be the Korn shell ('/usr/bin/ksh') but your script will run as the POSIX shell when your interpreter line reads:

#!/usr/bin/sh

(or)

#/bin/sh

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: how to change shell within the script

> I just want [...]

I don't know what that means. If the script
starts with "#!/bin/sh", then that's what
gets run to interpret the script.

> So in the script, I say echo $SHELL after
> /bin/shell line and shell is ksh.

If that bothers you, then you could set SHELL
to some other value (in your script). I
don't know how that will help you, but you
can do it.

Is there some actual problem which you are
trying to solve?
Bill Hassell
Honored Contributor

Re: how to change shell within the script

EVERY script must start with #! on the first line followed by the interpreter you want to use for your script. These are examples of different interpreters:

#!/usr/bin/sh
#!/usr/bin/ksh
#!/usr/bin/awk
#!/usr/local/bin/bash

and so on. A script is a set of instructions that are designed to be run by a specific program. Whether the program is also your shell or some other program is not important. If you do not have the #! line in your script, then you script will be run by the current shell -- not a good idea at all. Always specify the #! line for every script.


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: how to change shell within the script

> EVERY script must start with #! [...]

> If you do not have the #! line [...]

Make up your mind. (Or else don't say "must"
when you really mean "should".)
Dennis Handly
Acclaimed Contributor

Re: how to change shell within the script

>I say echo $SHELL after /bin/sh line and shell is ksh.

SHELL is set by login(1). And just about the only program that looks at $SHELL is make.
isunrise_1
New Member

Re: how to change shell within the script

bsh is posix shell
James R. Ferguson
Acclaimed Contributor

Re: how to change shell within the script

Hi (again):

> bsh is posix shell

Yes, it has Posix compliance. BUT: In HP-UX, the standard shell in '/usr/bin/sh' and in '/sbin/sh' is called the "Posix" shell. The important part is that while the 'bash' or the 'ksh' or other shell may be declared as a user's default shell, you should NEVER change root's default login shell. For root, only the statically linked '/sbin/sh' will allow you to reboot and startup a system (when '/usr' and the dynamic libraries every other shell needs) isn't mounted.

Regards!

...JRF...
mvpel
Trusted Contributor

Re: how to change shell within the script

Actually, Bill, not every script must start with #!.

There's a little trick you can use to get the shell to use a PATH search to find the Perl interpreter for you:

----
:
eval 'exec perl -S $0 ${1+"$@"}'
if $running_under_some_shell;

print "Hello world!\n";
----

I use this when some systems have Perl in /usr/local/bin and others have it in /usr/bin, when I haven't gotten around to symlinking.

The colon sends the script to sh, which runs the "eval/exec" line to fire up Perl on the same file, and then Perl ignores that line because "$running_under_some_shell" on the following line is not set, and proceeds down to the rest of the Perl script.
James R. Ferguson
Acclaimed Contributor

Re: how to change shell within the script

Hi (again):

> mvpel: Actually, Bill, not every script must start with #!. There's a little trick you can use to get the shell to use a PATH search to find the Perl interpreter for you:

A less obtuse way is simply:

#!/usr/bin/env perl

(or):

#!/usr/bin/env ksh

In which case we could still say that "every script must start with #!" :-)

Regards!

...JRF...