On 05/05/18 02:20, Assaf Gordon wrote:
> Hello,
> 
> Attached updated patch, two main improvements:
> 1. preserves empty arguments, e.g.
>         env -S echo "" bar
> 2. prints an error message on incorrect shebang usage:
> 
>       $ cat xxx
>       #!src/env -v -S cat -n
> 
>       $ ./xxx
>       src/env: possible incorrect usage of env in a script
>       Try 'src/env --help' for more information.
> 
> Item 2 is implemented in a separate patch (number 9) to ease review.
> 
> Of course the error message could be improved, as can the detection
> if the exact circumstances, but I'm not sure it's warranted.

I've attached some changes on top to indicate the existing error to users
plus additional info on how to address with the -S option.
I've also done this for the case of passing options to commands.
That results in:

  $ cat xxx
  #!src/env -i ls -l

  $ ./xxx
  src/env: invalid option -- ' '
  src/env: Use -[v]S to pass options in shebang lines
  Try 'src/env --help' for more information.

and

  $ cat xxx
  #!src/env ls -l

  $ ./xxx
  src/env: ‘ls -l’: No such file or directory
  src/env: Use -[v]S to pass options in shebang lines


> On 01/05/18 04:48 PM, Bernhard Voelker wrote:
>> On 05/01/2018 06:40 PM, Eric Blake wrote:
>>>
>>> #!/usr/bin/env -S -P /usr/bin:/opt/bin:${PATH} perl
>>>
> 
> "env -Ppath" is not implemented in this patch, but if we think it's
> worth while I'll add it in next patch.

It's best done in a separate patch.

thanks!
Pádraig
diff --git a/src/env.c b/src/env.c
index 74f50fd..2e60c2e 100644
--- a/src/env.c
+++ b/src/env.c
@@ -571,13 +571,11 @@ main (int argc, char **argv)
         case '-':
           /* Space,tab,dash are undocumented options. Attempt to detect
              incorrect shebang usage with extraneous space, e.g.:
-                #!env -v -S'cat -n'
-             In which case argv[1] == "-v -S'cat -n'" k.
-             see: https://lists.gnu.org/r/coreutils/2018-04/msg00020.html  */
+                #!/usr/bin/env -i command
+             In which case argv[1] == "-i command".  */
+          error (0, 0, _("invalid option -- '%c'"), optc);
           if (argc == 3)
-            error (0, 0, _("possible incorrect usage of env in a script"));
-          else
-            error (0, 0, _("invalid option -- '%c'"), optc);
+            error (0, 0, _("Use -[v]S to pass options in shebang lines"));
           usage (EXIT_CANCELED);
 
         case_GETOPT_HELP_CHAR;
@@ -659,5 +657,9 @@ main (int argc, char **argv)
 
   int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
   error (0, errno, "%s", quote (argv[optind]));
+
+  if (argc == 3 && exit_status == EXIT_ENOENT && strchr(argv[optind], ' '))
+    error (0, 0, _("Use -[v]S to pass options in shebang lines"));
+
   return exit_status;
 }

Reply via email to