Sujet : Re: Which shell and how to get started handling arguments
De : naddy (at) *nospam* mips.inka.de (Christian Weisgerber)
Groupes : comp.unix.shellDate : 15. Apr 2024, 14:35:40
Autres entêtes
Message-ID : <slrnv1qb9c.1cbm.naddy@lorvorc.mips.inka.de>
References : 1
User-Agent : slrn/1.0.3 (FreeBSD)
On 2024-04-15, James Harris <
james.harris.1@gmail.com> wrote:
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.
-- Christian "naddy" Weisgerber naddy@mips.inka.de