Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash and the bourne shell [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
b0nafide
Apprentice
Apprentice


Joined: 17 Feb 2008
Posts: 171
Location: ~/

PostPosted: Thu Sep 22, 2011 6:25 am    Post subject: bash and the bourne shell [solved] Reply with quote

Just noticed today that /bin/sh is symlinked on my system...

I was writing a script with #!/bin/sh for another system without bash and foolishly, I didn't realize I was using some bash features until after I'd run the script on the remote embedded system. I fixed it, but I was mildly disgruntled by the lack of sh on my system (I thought I was using /bin/sh all along).

There's no ebuild that I can find for the bourne shell? Another forum post suggested dash?

Perhaps the best thing to do is RTFM repeatedly until I've memorized which bash options are enhancements to the bourne shell? :) Or write and test such scripts on a different system with the bourne shell to begin with?


Last edited by b0nafide on Thu Sep 22, 2011 7:10 am; edited 1 time in total
Back to top
View user's profile Send private message
b0nafide
Apprentice
Apprentice


Joined: 17 Feb 2008
Posts: 171
Location: ~/

PostPosted: Thu Sep 22, 2011 7:09 am    Post subject: Reply with quote

Ahha!

Code:

#!/bin/busybox sh


So it's still lurking under the hood :)
Back to top
View user's profile Send private message
SlashBeast
Moderator
Moderator


Joined: 23 May 2006
Posts: 2843

PostPosted: Thu Sep 22, 2011 10:28 am    Post subject: Reply with quote

dash is the real sh, I have /bin/sh linked to /bin/dash. At this moment I don't know about any package what may fail to build with it, but few months ago there was multiple issues with it.

As many init scripts use bashism, what are executed by /bin/sh by default, will fail if you change sh to real posix sh. I have patched openrc to use /bin/bash instead. Here's patch https://raw.github.com/slashbeast/localpatch-patches/master/sys-apps/openrc/openrc_force_bash_for_initscripts.patch
_________________
BitBucket -- better-initramfs
Back to top
View user's profile Send private message
Ormaaj
Guru
Guru


Joined: 28 Jan 2008
Posts: 319

PostPosted: Thu Sep 22, 2011 11:18 am    Post subject: Reply with quote

SlashBeast wrote:
dash is the real sh

I think even Dash doesn't completely restrict it's functionality to sh. Busybox sh probably does and is closer to what you'd find on an embedded system anyway. If you wanna get real technical for a "real bourne shell" you'd be looking at something like the heirloom bourne shell. (OP probably doesn't actually want that) http://mywiki.wooledge.org/BourneShell

b0nafide wrote:
Perhaps the best thing to do is RTFM repeatedly until I've memorized which bash options are enhancements to the bourne shell?

If Bash is invoked as /bin/sh it goes into a mode which modifies all incompatible behaviors to be in line with POSIX, but doesn't disable those which are a superset so it can't be used to "validate" that your code doesn't contain extra Bashisms. AFAIK there aren't any shells that will do that 100% reliably other than to hope that incompatible syntax causes obvious exceptions or errors. You'd probably need some kind of lint tool which I'm not so sure exists. The shell grammar is just too complex and ambiguous. The only sure way is to learn the spec. Also, nice table of Bash invocation modes
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6279

PostPosted: Fri Sep 23, 2011 5:01 pm    Post subject: Reply with quote

Ormaaj wrote:
I think even Dash doesn't completely restrict it's functionality to sh.

First of all, there is not the sh - there are many systems with non-POSIX sh versions full of various bugs. Unless you have to support bugs of a particular system, I suggest that you use POSIX. In that case, dash is practically optimal: It has all POSIX functionalities, and only very limited additional functionalities: AFAIK the only additional functionality is the "local" keyword and the test operatations -nt -ot -ef.
Back to top
View user's profile Send private message
Ormaaj
Guru
Guru


Joined: 28 Jan 2008
Posts: 319

PostPosted: Fri Sep 23, 2011 11:27 pm    Post subject: Reply with quote

mv wrote:
Ormaaj wrote:
I think even Dash doesn't completely restrict it's functionality to sh.

First of all, there is not the sh - there are many systems with non-POSIX sh versions full of various bugs. Unless you have to support bugs of a particular system, I suggest that you use POSIX. In that case, dash is practically optimal: It has all POSIX functionalities, and only very limited additional functionalities: AFAIK the only additional functionality is the "local" keyword and the test operatations -nt -ot -ef.


I'm aware. I must have accidentally removed the POSIX qualifier sometime while editing that post. The point is that most shells like Bash even in sh mode and mostly conformant to POSIX sh still bend over backwards to be lenient of extra features/behaviors/syntax (from itself or obscure legacy shells) so long as it doesn't interfere with the conforming stuff, i.e. not strict, which is nice for development and testing new scripts. I'd bet even Dash isn't "ideal" in that sense, though it is probably as good as you're going to get.
Back to top
View user's profile Send private message
b0nafide
Apprentice
Apprentice


Joined: 17 Feb 2008
Posts: 171
Location: ~/

PostPosted: Sat Sep 24, 2011 1:11 am    Post subject: Reply with quote

For example:

Code:
$ /bin/bash --posix
$ awk '{ print $1 }' <<< "123 456"
123

$ /bin/busybox sh
$ awk '{ print $1 }' <<< "123 456"
sh: syntax error: unexpected redirection

$ /bin/dash
$ awk '{ print $1 }' <<< "123 456"
/bin/dash: Syntax error: redirection unexpected


So in my case dash would have caught the character redirection that I was starting to use out of habit in bash. If I understand correctly, as long as I'm not coding with the "local" keyword and the test operatations -nt -ot -ef , dash is the way to go for testing scripts designed for /bin/sh (in the absence of /bin/busybox).

Thanks for all the replies and great info.
Back to top
View user's profile Send private message
Ormaaj
Guru
Guru


Joined: 28 Jan 2008
Posts: 319

PostPosted: Sat Sep 24, 2011 7:40 am    Post subject: Reply with quote

Yep,
Code:
~ $ read -r x _ <<EOF
> 123 456
> EOF
> echo "$x"
123

I like herestrings though. :(
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6279

PostPosted: Sun Sep 25, 2011 8:38 pm    Post subject: Reply with quote

b0nafide wrote:
So in my case dash would have caught the character redirection

It would have caught that one. Unfortunately, bash introduced a completely incompatible redirection operator
Code:
something &>/dev/null
which POSIX shells interpret as
Code:
something & \
>/dev/null
with usually rather undesired side effects ("something" runs in the background without redirection, and the script processes immediately with exit status 0 from that command). So it might happen that you get unexpected results without any error message.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software 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