James Harris
2024-04-15 12:22:14 UTC
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!
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
James Harris