Austin Group Defect Tracker

Aardvark Mark IV


Viewing Issue Simple Details Jump to Notes ] Issue History ] Print ]
ID Category Severity Type Date Submitted Last Update
0000163 [1003.1(2008)/Issue 7] Shell and Utilities Editorial Error 2009-10-08 15:28 2013-04-16 13:06
Reporter msbrown View Status public  
Assigned To ajosey
Priority normal Resolution Accepted As Marked  
Status Closed  
Name Mark Brown
Organization IBM
User Reference
Section 2.14 trap
Page Number 2369-2370
Line Number 74930-74934
Interp Status ---
Final Accepted Text Note: 0000252
Summary 0000163: Problems in rationale of trap shell builtin
Description SUSv4> Set a trap so the logout utility in the directory referred
SUSv4> to by the HOME environment variable executes when the shell
SUSv4> terminates:
SUSv4>
SUSv4> trap '$HOME/logout' EXIT
      
That should be:

trap '"$HOME/logout"' EXIT
trap '~/logout' EXIT
($HOME expanded on exit)

or:

trap "$HOME/logout" EXIT
trap ~/logout EXIT
($HOME expanded upon evaluation of that trap command).

Later on:

SUSv4> The command:
SUSv4>
SUSv4> trap '$cmd' 0
SUSv4>
SUSv4> causes the contents of the shell variable cmd to be
SUSv4> executed as a command when the shell exits. Using
SUSv4> double-quotes instead of single-quotes might have
SUSv4> unexpected behavior, since in theory the value of cmd
SUSv4> might be a decimal integer which would be treated as
SUSv4> a condition, not an action; or cmd might begin with
SUSv4> '-' .

I find that misleading at best.

trap "$cmd" 0
(or trap ";$cmd" 0 if you want to account for possible $cmd's
that start with "-" or are decimal numbers which would be quite
odd)
($cmd meant to be shell code to be executed on exit)

trap '"$cmd"' 0
($cmd being the name of a command to be executed)

trap 'eval -- "$cmd"' 0
($cmd meant to be shell code to be executed on exit, but this
time $cmd is expanded upon the action execution).

all make sense to me. But trap '$cmd' 0 doesn't

For instance, in;

cmd='echo "* exit now *" >&2'
trap '$cmd' 0

assuming that $IFS has been unchanged upon exit would output the
list of filenames in the current directory that start with a
double-quote, followed by "exit now" followed by the filenames
that end with a double-quote (or *" if there's none), followed
by ">&2".

atexit='echo bye >&2'
trap "$atexit" 0

or:
atexit='echo bye >&2'
trap 'eval "$atexit" 0'
...
atexit='echo ciao >&2'
...

(though in that case, I'd prefer:

atexit() { echo bye >&2; }
trap atexit 0
...
atexit() { echo ciao >&2; }
)

make more sense.

SUSv4> Also, using double-quotes will cause the value
SUSv4> of cmd to be expanded twice, once when trap is
SUSv4> executed, and once when the condition arises.

Well no, it will be expanded upon evaluating the trap command,
and that expanded content will be evaluated upon exit, which is
quite the behavior I would expect.
Desired Action change
    trap '$HOME/logout' EXIT

to be
    trap '"$HOME/logout"' EXIT


($HOME expanded on exit) or:
    trap "$HOME/logout" EXIT


($HOME expanded upon evaluation of that trap command).


Tags tc1-2008
Attached Files

- Relationships
related to 0000252Closedajosey 1003.1(2008)/Issue 7 dot should follow Utility Syntax Guidelines 
related to 0001440Closed 1003.1(2016/18)/Issue7+TC2 Calling `system("-some-tool")` fails (although it is a valid `sh` command) 

-  Notes
(0000252)
geoffclare (manager)
2009-10-09 09:46
edited on: 2010-09-09 16:04

Proposed changes...
On line 74932 and line 74934 change

    '$HOME/logout'

to

    '"$HOME"/logout'

At line 74956 change

    The command:

    trap '$cmd' 0

    causes the contents of the shell variable cmd to be executed as a
    command when the shell exits.  Using double-quotes instead of
    single-quotes might have unexpected behavior, since in theory the
    value of cmd might be a decimal integer which would be treated as
    a condition, not an action; or cmd might begin with ’−’. Also,
    using double-quotes will cause the value of cmd to be expanded
    twice, once when trap is executed, and once when the condition
    arises.

to

    The command:

    trap 'eval " $cmd"' 0

    causes the contents of the shell variable cmd to be executed as a
    command when the shell exits.  Using

    trap '$cmd' 0

    does not work correctly if cmd contains any special characters
    such as quoting or redirections.  Using

    trap " $cmd" 0

    also works (the leading space character protects against unlikely
    cases where cmd is a decimal integer or begins with ’−’), but it
    expands the cmd variable when the trap command is executed, not
    when the exit action is executed.

At page 2343 line 74116 section eval, change the APPLICATION USAGE
section from

    None

to

    Since eval is not required to recognize the "--" end of options
    delimiter, in cases where the argument(s) to eval might begin
    with '-' it is recommended that the first argument is prefixed
    by a string that will not alter the commands to be executed,
    such as a space character:

    eval " $commands"

    or

    eval " $(some_command)"

At page 2344 line 74126 section eval, change the RATIONALE section from

    None

to

    This standard allows, but does not require, eval to recognize "--".
    Although this means applications cannot use "--" to protect against
    options supported as an extension (or errors reported for
    unsupported options), the nature of the eval utility is such that
    other means can be used to provide this protection (see APPLICATION
    USAGE above).



- Issue History
Date Modified Username Field Change
2009-10-08 15:28 msbrown New Issue
2009-10-08 15:28 msbrown Status New => Under Review
2009-10-08 15:28 msbrown Assigned To => ajosey
2009-10-08 15:28 msbrown Name => Mark Brown
2009-10-08 15:28 msbrown Organization => IBM
2009-10-08 15:28 msbrown Section => 2.14 trap
2009-10-08 15:28 msbrown Page Number => 2369-2370
2009-10-08 15:28 msbrown Line Number => 74930-74934
2009-10-08 15:28 msbrown Interp Status => ---
2009-10-08 15:32 msbrown Desired Action Updated
2009-10-08 15:33 msbrown Desired Action Updated
2009-10-09 09:46 geoffclare Note Added: 0000252
2009-10-15 15:34 msbrown Final Accepted Text => Note: 0000252
2009-10-15 15:34 msbrown Status Under Review => Resolved
2009-10-15 15:34 msbrown Resolution Open => Accepted As Marked
2010-08-27 13:07 ajosey Tag Attached: tc1-2008
2010-09-07 11:39 geoffclare Note Added: 0000542
2010-09-07 11:39 geoffclare Status Resolved => Under Review
2010-09-07 11:39 geoffclare Resolution Accepted As Marked => Reopened
2010-09-09 16:04 geoffclare Note Edited: 0000252
2010-09-09 16:04 geoffclare Note Deleted: 0000542
2010-09-09 16:05 geoffclare Status Under Review => Resolved
2010-09-09 16:05 geoffclare Resolution Reopened => Accepted As Marked
2013-04-16 13:06 ajosey Status Resolved => Closed
2016-02-23 21:36 eblake Relationship added related to 0000252
2021-11-04 15:28 eblake Relationship added related to 0001440


Mantis 1.1.6[^]
Copyright © 2000 - 2008 Mantis Group
Powered by Mantis Bugtracker