Subject: Re: bug#39135: Globbing with numbers does not allow me to specify order

From: Bob Proulx <[email-masked]>
Date: Wed, 22 Jan 2020 10:25:39 -0700

Antti Savolainen wrote:
> When doing a shortcut to unmount in a specific order, I am unable to
> specify order with angle brackets. For example using 'umount /dev/sda[132]'
> will result in the system unmounting them in numerological order. First 1
> then 2 and finally 3. What I need it to do is to first unmount 1, then 3
> and finally 2. It would be nice for the glob to respect the order of
> numbers that it was given.

As Bernhard wrote this involves features that have nothing to do with
coreutils.  However I thought I might say some more too.

You say you would like character class expansion of file globbing to
preserve the order.  But that isn't something that it has ever done
before all of the way back 40 years.  The [...] brackets give the file
glob parser (its called glob because wildcards can match a glob of
files) a list of characters to match.  These can be ranges such as A-Z
or 0-9 and so forth.  The collection effectively makes a set of
characters.  This is expanded by the command line shell.  To see the
expansion one can use the echo command to echo them out.  Try this to
see what a command like yours is doing.

  echo /dev/sda[132]

That shows what the umount command line arguments are going to be.
The command line shell expands the wildcards and then passes the
resulting expansion to the command.  The command never sees the wild
card itself.

Therefore your specific desire is that the command line shell would do
something different from what it is doing now.  And that would be
something different from what it has ever done in the past.  This
would be a new behavior and a change in historic behavior.  And almost
certainly one that would break someone who is now depending upon the
current behavior of sorting the arguments.  They would then file a bug
that the arguments were no longer being sorted.  And they were there
first by decades.  Therefore if I were maintaining a shell I would not
want to make changes to that ordering since it would certainly break
others and generate more bug reports.

Instead if you need to have things happen in a specific order then the
task is up to you to specify an explicit order.  Bernhard suggested
brace expansion, which is a GNU bash specific feature.

  echo /dev/sda{1,3,2}
  /dev/sda1 /dev/sda3 /dev/sda2

However I am not personally a fan of bash-isms in scripts.  They won't
work everywhere.  Therefore I personally would just explicitly specify
the order.

  umount /dev/sda1
  umount /dev/sda3
  umount /dev/sda2

Doing things that way is unambiguous.  And if that is the correct
order then it is the correct order.

If you need a command line short cut to make typing this in easier
then I personally would create a small shell script.

  #!/bin/sh
  # Unmount the devices in mount dependency order.
  umount /dev/sda1
  umount /dev/sda3
  umount /dev/sda2

Store this in /usr/local/bin/umount-sda or some such name that makes
sense to you and chmod a+x the file to make it executable.  Then it is
a documentable command to do exactly what is needed.  Typical command
line completion with TAB will help as a typing aid to expand the file
name for you.  That is the way I would do it.

Bob