0

I did a R package with a function that runs the "grep" system command. This package has been published on CRAN, and the tests on the Solaris platform return an error. This is likely due to a different usage of "grep" on Solaris. Here is an example of command run by the package:

grep --include=\*\.txt --colour=always -n -r -e 'PATTERN'

This command recursively searches the pattern 'PATTERN' in all 'txt' files find in the current folder and its subfolders, etc. (recursive search).

I don't have the error message generated by this command, I just know something wrong happens. What is the command equivalent to this one on Solaris?

1 Answers1

8

I believe the core of this problem is related to POSIX compatibility. grep is a tool that is specified by the POSIX standard. As schily points out in the comments, the POSIX specification of grep does not include the --include or --colour options. In fact, these options are part of GNU grep, which adds a lot more features to grep over and above what is mandated by the POSIX specification. The GNU grep manual can be found here: https://www.gnu.org/software/grep/manual/grep.html

On Solaris, the default grep tool is another POSIX-compatible implementation that is provided by Sun/Oracle. Naturally, that implementation does not support either of the options required here. The manpage for Solaris 10 can found here, and the one for Solaris 11 can found here.

This limitation goes beyond the grep command on Solaris. For example, the date command on Solaris is a lot less flexible compared to GNU date. The ps command on Solaris 10 also chops off process command-lines at 80 characters. As a solution/workaround to these problems, the GNU tools were eventually made available on Solaris 10 & 11. This QA goes into a lot more detail about where they can be found and how they can be installed.

When the GNU tools are installed on a Solaris system, and the name of the tool is already in use, a g is prefixed to command name. So, date becomes gdate, grep becomes ggrep and so on. In your case, the problem could be solved by using the ggrep command because it supports the required GNU-specific options (or extensions).

A possible problem here is that a Solaris system need not have the GNU tools installed. Some additional work is required to install these packages. If the GNU tools are not present on the target, then the ggrep command would simply be unavailable.

In general, a solution that aims for portability across POSIX distributions takes the opposite approach. Instead of using tools specific to each platform, the solution is intentionally modified to adhere to the POSIX specification, and to not use any extensions that are specific to any certain implementation. This is typically the only way to ensure that the solution works consistently across all POSIX-compatible systems.

Haxiel
  • 8,361