Liste des Groupes | Revenir à cu shell |
On 2024-04-15, James Harris <james.harris.1@gmail.com> wrote:To handle this deviation, always put an end‐of‐flags marker ("--", as specified by POSIX) before the first non‐flag argument, then even the GNU implementations will well‐behave, i. e. behave as specified by POSIX: Compare (using GNU ls) with Christians well‐behaving "ls": touch -- foo -l
>Q1) How can one write a script which is maximally compatible with different systems? >>
>
I am thinking to write in /the language of/ the Bourne shell, if feasible, so that it could be run by either the Bourne shell or Bash, etc? (Ideally, the shebang line would be #!/bin/sh.) >
Yes. POSIX shell, more specifically. That is the easy part. The
difficult part is that your script will likely call various external
commands and those have a lot of variation as well.
>Q2) How does one go about handling arguments in preferably a simple but universal way? >>
That's too vague... >
>I read up on getopts >>
If you want to handle option flags, getopts is the way to go. >
>but from tests it seems to require that switches precede arguments rather than allowing them to be specified after, so that doesn't seem very good, either. >>
But that's the way Unix commands work. You cannot specify flags
after the first non-flag argument.
>
$ touch foo -l
$ ls foo -l
-l foo
$ ls -l foo -l
-rw-r--r-- 1 naddy naddy 0 Apr 15 15:28 -l
-rw-r--r-- 1 naddy naddy 0 Apr 15 15:28 foo
>
Apparently GNU implementations deviate from this, which makes for a bad surprise and is incompatible with other implementations as well as historical practice. >
Les messages affichés proviennent d'usenet.