Groups | Search | Server Info | Login | Register


Groups > comp.unix.shell > #25067

Re: Which shell and how to get started handling arguments

From Helmut Waitzmann <nn.throttle@xoxy.net>
Newsgroups comp.unix.shell
Subject Re: Which shell and how to get started handling arguments
Date 2024-04-15 23:03 +0200
Organization A noiseless patient Spider
Message-ID <831q76bao7.fsf@helmutwaitzmann.news.arcor.de> (permalink)
References <uvj65n$9aep$1@dont-email.me> <slrnv1qb9c.1cbm.naddy@lorvorc.mips.inka.de>

Show all headers | View raw


 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. 

Back to comp.unix.shell | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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