- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: best shell for writing a shell script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 04:56 AM
01-22-2006 04:56 AM
While writing a shell script which shell is best to use:-
$ whereis sh
sh: /sbin/sh /usr/bin/sh /usr/newconfig/sbin/sh
$ whereis ksh
ksh: /usr/bin/ksh
I also noticed at various shell scripts use of #!/bin/sh
why born shell sh are located at different paths ? which is the real and correct one ?
Which is the best path to specify the shell path ?
Can i use ksh shell instead of sh ? what are the things i need to think while using ksh as shell for writing sys admin related scripts ?
Will it create an issue if a shell script written in korn shell executed from a born shell ?
Thanks,
Shiv
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 05:02 AM
01-22-2006 05:02 AM
SolutionLook at ls -ls {all shells} and you will see that almost all these files are the same.
Depending on the installation, you may have to different SH shells /bin/sh and /usr/bin/sh. /bin/sh is a small one and it works in single user mode.
ksh and sh are amost the same in HPUX, AFAIK. I did not see any difference when I used #!/usr/bin/sh and #!/usr/bin/ksh
It seems to me, there was a problem with transferring PATH to children processes in ksh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 05:43 AM
01-22-2006 05:43 AM
Re: best shell for writing a shell script
There is almost no difference between the Posix Shell and the Korn Shell. They are 99% alike.
I generally use /usr/bin/sh for the Posix shell. The one root uses has special functionality for root only. Though others can use it, root's should never be changed.
Korn is /usr/bin/ksh
I generally go with the right tool for the job approach, which means I use the shell that came with the product I'm working with. If Oracle scripts shipped with a particular shell, there is a finite possibility that they actually tested them under that shell on HP-UX(not 100% for certain).
In practical terms 99% of the time you can use Posix and Korn interchangibly.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 06:25 AM
01-22-2006 06:25 AM
Re: best shell for writing a shell script
The Korn shell and POSIX shell are all but identical. Despite its popularity, the one shell I tend to avoid is bash -- even on Linux. I have found that this construct:
echo "One Two Three" | read A B C
often fails under bash implementations; the most consistant shell for Linux that I have found is zsh.
- Tags:
- bash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 06:27 AM
01-22-2006 06:27 AM
Re: best shell for writing a shell script
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 06:37 AM
01-22-2006 06:37 AM
Re: best shell for writing a shell script
Everyone has their own idea of "best". After all, if that wasn't true then there wouldn't be any more than one variation of anything.
That said, I'll direct my answers to the HP-UX environment.
First, '/bin' is a deprecated path called a "transition link". Transition links are symbolic links dating to the HP-UX version 9-to-10 era when the the layout of the filesystems was redrawn. In 11i version 3 (not yet released) I believe that these links will finally disappear. Hence, do not use them. That is, don't write "/bin/sh". Instead write "/usr/bin/sh".
The default shell for HP-UX is the Posix shell which lives as '/usr/bin/sh' or as '/sbin/sh'. The difference is that the shell binary in '/usr/bin' uses dynamically linked libraries (ones whose linkage is resolved at runtime) whereas the '/sbin/sh' binary uses statically linked libraries (built at compile-time).
The advantage to having dynamic libraries is that multiple users share some common code and therefore the overall memory footprint of each instantiation of the shell is less than multiple users of the statically linked shell.
However, since '/usr' isn't mounted in single-user mode; and/or when scripts are run during bootup *before* '/usr' is mounted; a statically linked binary with everything tidily set is a requirement during bootup. This is the reason that the '/sbin/sh' binary should *always* be the one used for 'root'. If you fail to heed the advise, you would find that your system won't startup.
The Posix shell is exceedly close to a Korn shell and the two are virtually interchangeable. You can find a Korn88 shell in '/usr/bin/ksh' and a Korn93 shell in '/usr/dt/bin/dtksh'.
The C shell is lives in '/usr/bin/csh', but I wouldn't use it at all. There are a multitude of resaons why *not* to use it, and they are well enumerated here:
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
The very, old, Bourne shell can still be found as '/usr/old/bin/sh' but is virtually useless.
Other shells have been ported to HP-UX. The "Boune-Again_Shell" ('bash') is one popular one. Generally, these, and others often would be installed in '/usr/bin'. Exactly where they live matters less than the fact that the scripts that use them would declare their interpreter with the "she-bank" sequence -- "#!/usr/bin/bash" for instance, to tell that the script is a 'bash' shell script. The "she-bang" characters (#!) tell the kernel that what follows is the interpreter, or program to use.
A very good site for shell scripts and tips is:
http://www.shelldorado.com/
A guide to shells can be found here:
http://docs.hp.com/en/B2355-90046/index.html
All shell scripts start with a declaration of the shell interpreter, clearly identifing the shell in use, like this:
#!/usr/bin/sh
#!/usr/bin/ksh
#!/usr/dt/bin/dtksh
Any script, written in any shell languare, or any binary (written in any language) can always be called from a shell.
Regards!
...JRF...
- Tags:
- shelldorado
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 06:43 AM
01-22-2006 06:43 AM
Re: best shell for writing a shell script
Please see my last post here, too:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=992587
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 09:50 AM
01-22-2006 09:50 AM
Re: best shell for writing a shell script
regards,
shiv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 09:51 AM
01-22-2006 09:51 AM
Re: best shell for writing a shell script
regards,
shiv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 10:13 AM
01-22-2006 10:13 AM
Re: best shell for writing a shell script
You commented, that "[A] few years ago; i started a process in different shell which was written in some other shell. I don't recall exact error but it created some issue."
I would suspect that there were variables defined in one script that you expected to be available in the environment of the other but were not.
For instance, if script-1 needs to make script-2 aware of a variable when it launches script-2, then script-1 needs to 'export' the variable first:
# cat script1
echo "hello"
export WHO=Shiv
# cat script2
echo "I am ${WHO}
Now script2 would report that it is "Shiv".
Another way of sharing variables is to pass them as arguments between scripts. Then, $1, $2...$n represent the arguments:
# ./script2 Shiv
...runs ./script2 passing "Shiv" as $1.
Yet another way to share variables between scripts is to include a file in a shell script. The process is called "sourcing" a file. You cause this by writing a "dot" followed by a "blank" followed by the file name:
#/usr/bin/sh
echo "the next line says include the file"
. $HOME/includeme
echo "done"
# cat $HOME/includeme
WHO=Shiv
This technique is often used to include environmental variables from a common file into a script instead of declaring them in a login profile.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 10:27 AM
01-22-2006 10:27 AM
Re: best shell for writing a shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 12:05 PM
01-22-2006 12:05 PM
Re: best shell for writing a shell script
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 02:54 PM
01-22-2006 02:54 PM
Re: best shell for writing a shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 07:14 PM
01-22-2006 07:14 PM
Re: best shell for writing a shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 07:23 PM
01-22-2006 07:23 PM
Re: best shell for writing a shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 07:29 PM
01-22-2006 07:29 PM
Re: best shell for writing a shell script
http://www.askdavetaylor.com/whats_acceptable_syntax_in_unix_and_linux_sh_scripts.html
Hope this link will be useful.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2006 07:51 PM
01-22-2006 07:51 PM
Re: best shell for writing a shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2006 04:22 AM
01-23-2006 04:22 AM
Re: best shell for writing a shell script
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.
- Tags:
- scummy C shell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2006 04:35 AM
01-23-2006 04:35 AM
Re: best shell for writing a shell script
That is the POSIX shell (on HPUX) and will work with KSH scripting standards.
I usually use #!/bin/sh less typing :)
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2006 05:11 AM
01-23-2006 05:11 AM
Re: best shell for writing a shell script
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