Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Write true sh (rather than bash) script on linux

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
pc



Joined: Fri Apr 08, 2005 6:12 pm
Posts: 24

PostPosted: Sat Oct 14, 2006 4:09 pm    Post subject: Write true sh (rather than bash) script on linux Reply with quote

Solaris by default has a true "sh" shell.
Linux by default seems to have the sh shell as link to the bash shell.

If I want to write a truely portable script, I would like to ensure that it is "sh" compatible, and does not use any of the extra features from bash.

Can anyone advise of a way of writing a true sh script on linux, that does not seem to come with a true sh shell?
Back to top
View user's profile Send private message
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Sun Oct 15, 2006 12:46 am    Post subject: RE: Write true sh (rather than bash) script on linux Reply with quote

google??
_________________
[url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url]
Back to top
View user's profile Send private message Visit poster's website
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Sun Oct 15, 2006 12:32 pm    Post subject: RE: Write true sh (rather than bash) script on linux Reply with quote

Actually, Solaris has several shells available, including bash. It's installed by default with Solaris-10, although it may not be available for previous versions.
So if you write a bash script and make sure it has the line
Code:
#!/bin/bash

at the top, it should work fine on Linux and Solaris-10 (and OS-X).

Prior to Solaris-10, if I wanted a portable shell script I would use csh - that was the default on some old versions of Unix and appears to be universally available still.
_________________
Hope this helps,

Nigel.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8453
Location: Warrington, UK

PostPosted: Sun Oct 15, 2006 2:44 pm    Post subject: RE: Write true sh (rather than bash) script on linux Reply with quote

When bash is executed through the sh link, it behaves as sh. I generally write scripts with a /bin/sh shebang line unless it uses something bash-specific.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
pc



Joined: Fri Apr 08, 2005 6:12 pm
Posts: 24

PostPosted: Mon Oct 16, 2006 10:10 pm    Post subject: RE: Write true sh (rather than bash) script on linux Reply with quote

Thanks for the replies. The solaris is version 9 and bash is not I believe a default.

I didn't realise that bash through an sh link behaves like sh. How does it do that? How does it know it being called through a link with the name "sh" and so behaves differently?

Quick look at man bash gives:
If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well.

Guess I should have read the help more thoroughly, apologies.

So the simple answer seem to be to start the script with:

#!/bin/sh

and on linux it will interpret it as "true" sh, even though /bin/sh is a link to /bin/bash.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8453
Location: Warrington, UK

PostPosted: Mon Oct 16, 2006 10:53 pm    Post subject: Re: RE: Write true sh (rather than bash) script on linux Reply with quote

pc wrote:
I didn't realise that bash through an sh link behaves like sh. How does it do that? How does it know it being called through a link with the name "sh" and so behaves differently?


Because each program is passed the name it was called with as its first argument. Write a shell script that contains "echo $0", make a couple of symlinks to it and run them to see what happens.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
Steogede
LXF regular


Joined: Thu May 04, 2006 6:39 pm
Posts: 146

PostPosted: Mon Oct 16, 2006 11:45 pm    Post subject: Re: RE: Write true sh (rather than bash) script on linux Reply with quote

pc wrote:
I didn't realise that bash through an sh link behaves like sh. How does it do that? How does it know it being called through a link with the name "sh" and so behaves differently?


Did no one ever tell you bash is omniscient?
Back to top
View user's profile Send private message
pc



Joined: Fri Apr 08, 2005 6:12 pm
Posts: 24

PostPosted: Wed Oct 18, 2006 10:29 pm    Post subject: RE: Re: RE: Write true sh (rather than bash) script on linux Reply with quote

nelz,

Thnaks for the explanation. I did indeed create a script and a soft link to that script. $0 was the script name when run directly, and the link name when run via the link. Previously I had taken $0 to always be the script name, which I can see now is not always the case.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast