* On Monday 2005-10-24 at 15:58:30 +0100, Julian Foad wrote: > Dan Jacobson wrote: > >$ grep . /;echo $? > >1 > >$ strace grep . / > >...open("/", O_RDONLY|O_LARGEFILE) = 3 > >fstat64(3, {st_mode=S_IFDIR|0755, st_size=1024, ...}) = 0 > >read(3, 0x8071000, 32768) = -1 EISDIR (Is a directory)... > > For comparison, here is how it handles other problems with reading the > files: > > >$ grep . @; echo $? > >grep: @: No such file or directory > >2
According to the grep(1p) POSIX "man page": ] EXIT STATUS ] The following exit values shall be returned: ] ] 0 One or more lines were selected. ] 1 No lines were selected. ] >1 An error occurred. ] ] CONSEQUENCES OF ERRORS ] If the -q option is specified, the exit status ] shall be zero if an input line is selected, ] even if an error was detected. This does not restrict GNU grep from being more informative about what happened by documenting and using many exit statuses that are greater than 1 (e.g., open() error vs. read() error), using either successive values (2, 3, 4, 5, ...) or bit flags (2, 4, 8, ...). > On the other hand, I wonder whether this behaviour was intentional. The > "info" help text says slightly more than the "man" page: > > >`--directories=ACTION' > > If an input file is a directory, use ACTION to process it. By > > default, ACTION is `read', which means that directories are read > > just as if they were ordinary files (some operating systems and > > filesystems disallow this, and will cause `grep' to print error > > messages for every directory or silently skip them). > > I don't know: it all seems a bit arbitrary and inconsistent. What's the > point of defaulting to ACTION="read" on systems that don't support it? Consistency (for grep) across OSes. If OSes are inconsistent, that is another issue; grep should merely use its exit status to document what actually happened at a lower level on the OS on which it happens to be running.