* Replacement for declare in dash?
@ 2013-10-16 12:18 Alexander Huemer
2013-10-16 13:09 ` Guido Berhoerster
2013-10-16 14:17 ` Chet Ramey
0 siblings, 2 replies; 5+ messages in thread
From: Alexander Huemer @ 2013-10-16 12:18 UTC (permalink / raw)
To: dash
Hi,
I wrote a shell script on a non-Debian system and assumed, in my
greenness, that testing it with #!/bin/bash --posix would ensure that
the script would run under dash too. I was wrong. Of course that is the
fault of bash and not dash.
Here is a simplified form of the script:
#!/bin/dash
# This works when run with #!/bin/bash --posix
func() {
cat /proc/1/environ
}
sudo sh -c "$(declare -f func); func"
Can I do anything equivalent with dash?
I did not find anything in the docs, but hope dies last.
Of course I can swap the function out in a seperate script, but I'd like
to avoid that.
Thanks in advance for all ideas.
Kind regards,
-Alex
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Replacement for declare in dash?
2013-10-16 12:18 Replacement for declare in dash? Alexander Huemer
@ 2013-10-16 13:09 ` Guido Berhoerster
2013-10-16 14:17 ` Chet Ramey
1 sibling, 0 replies; 5+ messages in thread
From: Guido Berhoerster @ 2013-10-16 13:09 UTC (permalink / raw)
To: dash
* Alexander Huemer <alexander.huemer@xx.vu> [2013-10-16 14:26]:
> Hi,
>
> I wrote a shell script on a non-Debian system and assumed, in my
> greenness, that testing it with #!/bin/bash --posix would ensure that
> the script would run under dash too. I was wrong. Of course that is the
> fault of bash and not dash.
> Here is a simplified form of the script:
>
> #!/bin/dash
> # This works when run with #!/bin/bash --posix
>
> func() {
> cat /proc/1/environ
> }
>
> sudo sh -c "$(declare -f func); func"
>
> Can I do anything equivalent with dash?
No.
> I did not find anything in the docs, but hope dies last.
This is even ugly in bash because you're abusing a function as a
variable. And the shell you get with "sudo sh" depends on your
system configuration.
You can embed commands using a here-doc instead of func()...
sudo /bin/dash << "EOF"
cat /proc/1/environ
# ...
EOF
> Of course I can swap the function out in a seperate script, but I'd like
> to avoid that.
...but this seems to be the right thing to do rather than
embedding a script inside a script.
--
Guido Berhoerster
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Replacement for declare in dash?
2013-10-16 12:18 Replacement for declare in dash? Alexander Huemer
2013-10-16 13:09 ` Guido Berhoerster
@ 2013-10-16 14:17 ` Chet Ramey
2013-10-16 14:41 ` Eric Blake
2013-10-16 15:34 ` Alexander Huemer
1 sibling, 2 replies; 5+ messages in thread
From: Chet Ramey @ 2013-10-16 14:17 UTC (permalink / raw)
To: Alexander Huemer; +Cc: dash, chet.ramey
On 10/16/13 8:18 AM, Alexander Huemer wrote:
> Hi,
>
> I wrote a shell script on a non-Debian system and assumed, in my
> greenness, that testing it with #!/bin/bash --posix would ensure that
> the script would run under dash too. I was wrong. Of course that is the
> fault of bash and not dash.
Maybe you should have verified your assumptions first. That is simply not
what the --posix option does. Posix mode makes bash a superset of Posix;
it's not a nothing-but-what-posix-specifies mode.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Replacement for declare in dash?
2013-10-16 14:17 ` Chet Ramey
@ 2013-10-16 14:41 ` Eric Blake
2013-10-16 15:34 ` Alexander Huemer
1 sibling, 0 replies; 5+ messages in thread
From: Eric Blake @ 2013-10-16 14:41 UTC (permalink / raw)
To: chet.ramey, Alexander Huemer; +Cc: dash
[-- Attachment #1: Type: text/plain, Size: 782 bytes --]
On 10/16/2013 08:17 AM, Chet Ramey wrote:
> On 10/16/13 8:18 AM, Alexander Huemer wrote:
>> Hi,
>>
>> I wrote a shell script on a non-Debian system and assumed, in my
>> greenness, that testing it with #!/bin/bash --posix would ensure that
>> the script would run under dash too. I was wrong. Of course that is the
>> fault of bash and not dash.
>
> Maybe you should have verified your assumptions first. That is simply not
> what the --posix option does. Posix mode makes bash a superset of Posix;
> it's not a nothing-but-what-posix-specifies mode.
If you want a shell that does nothing-but-what-posix-specifies, use posh
from the Debian project.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Replacement for declare in dash?
2013-10-16 14:17 ` Chet Ramey
2013-10-16 14:41 ` Eric Blake
@ 2013-10-16 15:34 ` Alexander Huemer
1 sibling, 0 replies; 5+ messages in thread
From: Alexander Huemer @ 2013-10-16 15:34 UTC (permalink / raw)
To: Chet Ramey; +Cc: dash
On Wed, Oct 16, 2013 at 10:17:26AM -0400, Chet Ramey wrote:
> On 10/16/13 8:18 AM, Alexander Huemer wrote:
> > Hi,
> >
> > I wrote a shell script on a non-Debian system and assumed, in my
> > greenness, that testing it with #!/bin/bash --posix would ensure that
> > the script would run under dash too. I was wrong. Of course that is the
> > fault of bash and not dash.
>
> Maybe you should have verified your assumptions first. That is simply not
> what the --posix option does. Posix mode makes bash a superset of Posix;
> it's not a nothing-but-what-posix-specifies mode.
Thanks for your answer and also for the answers of Guido Berhoerster and
Eric Blake. That's correct, my assumption was wrong. I'll try to use the
cat << trick, let's see if that works, I won't assume anything at the
moment ;) Also thanks for the hint regarding posh, I didn't know that.
Kind regards,
-Alex
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2013-10-16 15:34 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-16 12:18 Replacement for declare in dash? Alexander Huemer
2013-10-16 13:09 ` Guido Berhoerster
2013-10-16 14:17 ` Chet Ramey
2013-10-16 14:41 ` Eric Blake
2013-10-16 15:34 ` Alexander Huemer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).