Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.unix.shell > #25061
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.unix.shell |
| Subject | Re: Which shell and how to get started handling arguments |
| Date | 2024-04-15 15:45 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87r0f6ptve.fsf@bsb.me.uk> (permalink) |
| References | <uvj65n$9aep$1@dont-email.me> |
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.
Back to comp.unix.shell | Previous | Next — Previous in thread | Next in thread | Find similar
Which shell and how to get started handling arguments James Harris <james.harris.1@gmail.com> - 2024-04-15 13:22 +0100
Re: Which shell and how to get started handling arguments Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-15 16:03 +0200
Re: Which shell and how to get started handling arguments Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-15 16:34 +0200
Re: Which shell and how to get started handling arguments Christian Weisgerber <naddy@mips.inka.de> - 2024-04-15 13:35 +0000
Re: Which shell and how to get started handling arguments Helmut Waitzmann <nn.throttle@xoxy.net> - 2024-04-15 23:03 +0200
Re: Which shell and how to get started handling arguments Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-16 01:14 +0000
Re: Which shell and how to get started handling arguments Helmut Waitzmann <nn.throttle@xoxy.net> - 2024-04-16 22:23 +0200
Re: Which shell and how to get started handling arguments Ben Bacarisse <ben.usenet@bsb.me.uk> - 2024-04-15 15:45 +0100
Re: Which shell and how to get started handling arguments Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-04-15 15:06 +0000
Re: Which shell and how to get started handling arguments Christian Weisgerber <naddy@mips.inka.de> - 2024-04-15 15:36 +0000
Re: Which shell and how to get started handling arguments gazelle@shell.xmission.com (Kenny McCormack) - 2024-04-15 17:38 +0000
Re: Which shell and how to get started handling arguments Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-15 17:57 +0000
Re: Which shell and how to get started handling arguments Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-04-15 20:37 +0000
Re: Which shell and how to get started handling arguments Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-15 14:31 -0700
Re: Which shell and how to get started handling arguments Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-04-16 10:19 +0200
Re: Which shell and how to get started handling arguments Christian Weisgerber <naddy@mips.inka.de> - 2024-04-16 11:11 +0000
Re: Which shell and how to get started handling arguments Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-16 11:54 -0700
Re: Which shell and how to get started handling arguments gazelle@shell.xmission.com (Kenny McCormack) - 2024-04-16 19:59 +0000
Re: Which shell and how to get started handling arguments Christian Weisgerber <naddy@mips.inka.de> - 2024-04-16 21:57 +0000
Re: Which shell and how to get started handling arguments Kaz Kylheku <643-408-1753@kylheku.com> - 2024-04-16 20:45 +0000
Re: Which shell and how to get started handling arguments Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-04-16 16:38 -0700
Re: Which shell and how to get started handling arguments Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-17 00:52 +0000
Re: Which shell and how to get started handling arguments gazelle@shell.xmission.com (Kenny McCormack) - 2024-04-17 09:02 +0000
Re: Which shell and how to get started handling arguments Christian Weisgerber <naddy@mips.inka.de> - 2024-04-17 14:23 +0000
Re: Which shell and how to get started handling arguments Helmut Waitzmann <nn.throttle@xoxy.net> - 2024-04-15 23:49 +0200
Re: Which shell and how to get started handling arguments Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-04-15 22:20 +0000
csiph-web