Re: Which shell and how to get started handling arguments

Liste des GroupesRevenir à cu shell 
Sujet : Re: Which shell and how to get started handling arguments
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shell
Date : 15. Apr 2024, 23:03:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <831q76bao7.fsf@helmutwaitzmann.news.arcor.de>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
 Christian Weisgerber <naddy@mips.inka.de>:
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. >
 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
   $ ls foo -l
   -rw------- 1 helmut helmut 0 Apr 15 15:28 foo
 which deviates from POSIX,    $ ls -l foo -l
   -rw------- 1 helmut helmut 0 Apr 15 15:28 foo
 which deviates from POSIX,    $ ls -- foo -l
   -l
   foo
 which behaves as specified by POSIX,    $ ls -l -- foo -l
   -rw------- 1 helmut helmut 0 Apr 15 15:28 -l
   -rw------- 1 helmut helmut 0 Apr 15 15:28 foo
 which behaves as specified by POSIX.
Date Sujet#  Auteur
15 Apr 24 * Which shell and how to get started handling arguments26James Harris
15 Apr 24 +* Re: Which shell and how to get started handling arguments2Janis Papanagnou
15 Apr 24 i`- Re: Which shell and how to get started handling arguments1Janis Papanagnou
15 Apr 24 +* Re: Which shell and how to get started handling arguments4Christian Weisgerber
15 Apr 24 i`* Re: Which shell and how to get started handling arguments3Helmut Waitzmann
16 Apr 24 i `* Re: Which shell and how to get started handling arguments2Kaz Kylheku
16 Apr 24 i  `- Re: Which shell and how to get started handling arguments1Helmut Waitzmann
15 Apr 24 +- Re: Which shell and how to get started handling arguments1Ben Bacarisse
15 Apr 24 +* Re: Which shell and how to get started handling arguments16Lew Pitcher
15 Apr 24 i+* Re: Which shell and how to get started handling arguments4Christian Weisgerber
15 Apr 24 ii+* Re: Which shell and how to get started handling arguments2Kenny McCormack
15 Apr 24 iii`- Re: Which shell and how to get started handling arguments1Kaz Kylheku
15 Apr 24 ii`- Re: Which shell and how to get started handling arguments1Lew Pitcher
15 Apr 24 i`* Re: Which shell and how to get started handling arguments11Keith Thompson
16 Apr 24 i +- Re: Which shell and how to get started handling arguments1Janis Papanagnou
16 Apr 24 i `* Re: Which shell and how to get started handling arguments9Christian Weisgerber
16 Apr 24 i  +* Re: Which shell and how to get started handling arguments3Keith Thompson
16 Apr 24 i  i`* Re: Which shell and how to get started handling arguments2Kenny McCormack
16 Apr 24 i  i `- Re: Which shell and how to get started handling arguments1Christian Weisgerber
16 Apr 24 i  +* Re: Which shell and how to get started handling arguments2Kaz Kylheku
17 Apr 24 i  i`- Re: Which shell and how to get started handling arguments1Keith Thompson
17 Apr 24 i  `* Re: Which shell and how to get started handling arguments3Lawrence D'Oliveiro
17 Apr 24 i   +- Re: Which shell and how to get started handling arguments1Kenny McCormack
17 Apr 24 i   `- Re: Which shell and how to get started handling arguments1Christian Weisgerber
15 Apr 24 +- Re: Which shell and how to get started handling arguments1Helmut Waitzmann
16 Apr 24 `- Re: Which shell and how to get started handling arguments1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal