Sujet : Which shell and how to get started handling arguments
De : james.harris.1 (at) *nospam* gmail.com (James Harris)
Groupes : comp.unix.shellDate : 15. Apr 2024, 14:22:14
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uvj65n$9aep$1@dont-email.me>
User-Agent : Mozilla Thunderbird
For someone who is relatively new to Unix shell scripting (me) some advice would be more than welcome on where to begin.
I have two main queries:
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.)
Or is Bash now so universal that there's no point any longer in writing for anything else?
Q2) How does one go about handling arguments in preferably a simple but universal way?
My first idea was to iterate over arguments with such as
while [ $# -gt 0 ]
do
...
shift
done;
and from that to (a) note any switches and (b) build up an array of positional parameters. However, I gather the Bourne shell has no arrays (other than the parameters themselves) so that won't work.
I read up on getopts 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.
Online tutorials show different ways to handle this and few talk about which shell to use for this case so I thought I would ask you guys for suggestions.
My requirement just now is, in fact, so simple that I don't need a universal way to handle things but ISTM best to start with an approach that will scale over time, if there is one.
So any guidance on how to get started would be appreciated!
--
James Harris