Operating System - Linux
1753407 Members
7014 Online
108793 Solutions
New Discussion юеВ

Re: best shell for writing a shell script

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Re: best shell for writing a shell script

Hi James;

Suppose a shell script written by someone is using #!/usr/bin/sh (which we can file by viewing the script) and my default shell is #!/usr/bin/ksh; do you suggest it is better to change my shell in #!/usr/bin/sh before executing that script ?

warm regards,
shiv

James R. Ferguson
Acclaimed Contributor

Re: best shell for writing a shell script

Hi Shiv:

You wrote: "Suppose a shell script written by someone is using #!/usr/bin/sh (which we can file by viewing the script) and my default shell is #!/usr/bin/ksh; do you suggest it is better to change my shell in #!/usr/bin/sh before executing that script ?"

There is no reason to change your default login shell, which you have defined in 'etc/passwd', for you account, to be the Korn shell.

When you run a Posix shell script, its instructions are interpreted by the Posix shell because you *specified* '/usr/bin/sh' as the interpreter in the first line of your script. This is correct form and will yield an execution consistent with the declared interpreter.

Regards!

...JRF...
Arunvijai_4
Honored Contributor

Re: best shell for writing a shell script

Hi Shiv,

You got all the answers from ITRC gurus. We always use /usr/bin/ksh n our shell scripts since we had to follow some coding standards. It varies across organisations, some prefer to use /sbin/sh since it will be available even in single user mode.

Hope this is clear.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
dirk dierickx
Honored Contributor

Re: best shell for writing a shell script

you can use any shell you want for writing your scripts. the reason why a lot of scripts are using /bin/sh is that is is the basic script that should be available at all times (so it is the best choice for init scripts). sh is available on all unix systems, including linux where ksh is not installed by default. so it's possible to write an 'sh' script that works for all unix versions.

i use 'sh' for all my scripts as well. there is not much it can not do, and well, if a scripts is to large i just switch to perl.
Muthukumar_5
Honored Contributor

Re: best shell for writing a shell script

When you are using shell type then it has to be available in user mode. /usr file system will not there in single user mode. You have to use /sbin/sh only there.

When you are using in 3 or 5th run level then based on the requirement you can use ksh or sh or bash (if you are installed). It is better to go with ksh in hp-ux.

If you are not changing shell format in shell script then you will not get any error when executing ksh script in sh mode. Else it will prompt errors.

--
Muthu

Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: best shell for writing a shell script

Hi Shiv,

http://www.askdavetaylor.com/whats_acceptable_syntax_in_unix_and_linux_sh_scripts.html

Hope this link will be useful.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Yogeeraj_1
Honored Contributor

Re: best shell for writing a shell script

hi shiv,

if you were to start writing your own script, i would agree with the comment above that it is preferrable that you start learning perl!

it is much more powerful

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
rmueller58
Valued Contributor

Re: best shell for writing a shell script

if you write to Bourne shell, most shell interpreters will handle the command. I've had to write specific scripts because of handling certain types of functions in BASH or KSH..

I prefer not to use CSH because nothing fits except C. KSH on UX or BASH on Linux is probably the best route to follow.

"#which ksh" will define where the interpreter sits.

A K-shell script may or may not work under bourne.. variables may need to be reset, via a sub shell.
Geoff Wild
Honored Contributor

Re: best shell for writing a shell script

/bin/sh is symbolic link to /usr/bin/sh

That is the POSIX shell (on HPUX) and will work with KSH scripting standards.

I usually use #!/bin/sh less typing :)

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Mark Ellzey
Valued Contributor

Re: best shell for writing a shell script

Shiv,

It doesn't matter what your login shell is when you execute a script that has the 'she-bang' line at the top of the script. The script that you execute will always be run under the the shell listed in the 'she-bang'.

For example, I administer a number of sites where startup/install/configuration scripts for our software package are written in csh (ugh!). I use the POSIX shell (/usr/bin/sh) as my login shell, and when I execute one of those scripts, the subshell they execute in is csh.

If you ever edited the root crontab with crontab -e, you've noticed that when you file (:wq) it, a message is displayed:
warning: commands will be executed using /usr/bin/sh
This means that cron will use /usr/bin/sh to run the commands. However, if you are executing a script that has #!/usr/bin/csh as the first line of the script, it will spawn a csh subshell to execute the script in.

Cheers,
Mark