Sujet : Re: Which shell and how to get started handling arguments
De : ben.usenet (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.unix.shellDate : 15. Apr 2024, 15:45:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87r0f6ptve.fsf@bsb.me.uk>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13)
James Harris <
james.harris.1@gmail.com> writes:
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?
Use only the features described for POIX sh.
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.)
The term "Bourne shell" is a little ambiguous. Many people take it to
mean "POSIX shell" but some people would go further and take it to mean
an older shell without some of the most recent things you can not rely
on.
Or is Bash now so universal that there's no point any longer in writing for
anything else?
That depends on the your audience. For Linux users, pretty much yes.
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.
Well that's what most people will be used to. I would want
command -o out1 file1 -z -i out2 file2
to use out1 for the first file, out2 for the second and for the -z to
apply only to the second file.
If you can accept that this is a reasonable way of working, then you can
use your previously written loop. Every non-flag argument is just
processed from inside the loop at the point it is seen.
If you have to save them for later, you could consider building a
string of saved arguments using an "unlikely" separator string:
#!/bin/sh
args=""
sep='
'
while [ $# -gt 0 ]
do
case "$1" in
-*) echo flag: $1
;;
*) args="$1$sep$args"
;;
esac
shift
done
while [ -n "${args}" ]
do
echo "Arg is '${args%%$sep*}'"
args="${args#*$sep}"
done
This reverses the order. You can preserve the order with slightly
different string fiddling.
-- Ben.