Summary: | sys-apps/gentoo-functions-0.8: "shift" and "type" bugs/bashisms | ||
---|---|---|---|
Product: | Gentoo Linux | Reporter: | Martin Väth <martin> |
Component: | [OLD] Core system | Assignee: | William Hubbs <williamh> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | base-system, blueness, kfm |
Priority: | Normal | ||
Version: | unspecified | ||
Hardware: | All | ||
OS: | Linux | ||
Whiteboard: | |||
Package list: | Runtime testing required: | --- | |
Bug Depends on: | |||
Bug Blocks: | 526268 |
Description
Martin Väth
2014-12-23 09:14:58 UTC
The first two issues listed are fixed in commit 6ad1c02. This will be included in release 0.9. I am not concerned at this time with the use of local. It is true that this is not POSIX, but as stated above it should not be a problem. If I find that it is a problem later, I will look into it. (In reply to Martin Väth from comment #0) > 3. "local" is not POSIX. However, for this I don't know a workaround, and it > is usually not a problem unless you use a very ancient shell which also > lacks other POSIX features needed by gentoo-functions. I realise that this is an old comment but it caught my interest. It can often be worked around by having the compound command that constitutes the function body be of the kind that creates a subshell, rather than the kind that does not. For example, this ... f() { local a a=123 } ... might be written as this ... f() ( a=123 ) Of course, this method is only suitable where it is acceptable to dispense with any changes in state incurred by the subshell. In particular, don't proceed to alter variables that were defined on some other scope and expect the parent shell to be any the wiser. Still, it's surprising how much code there is out there where it is turns out not to be an issue. Indeed, losing state changes is often useful (think set -f, IFS changes and so forth). As such, I wish that the technique were more often employed. It seems that people have it hard-wired into their brains that function 'bodies' are defined by the curly braces but it's simply not true of the shell. Furthermore, but there are two pitfalls with local that I see people walking into time and time again. The first is that it results in the exit status value of a command substitution being lost. One needs to declare the variable and perform the assignment separately to avoid that. The second is that, depending on the exact implementation, one may need to quote the right-hand-side, even in situations where it _wouldn't_ have been necessary without local. The netifrc project is an example of one that is peppered with potential bugs as a result of not taking into consideration these nuances. See bug 528314 as an example; now well over 6 years old. What's the easiest solution? Not to use local and to be serious about POSIX conformance, as it happens. Either that, or to stop pretending and target a specific shell. |