[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: default/simple globbing
From: |
Andreas Kähäri |
Subject: |
Re: default/simple globbing |
Date: |
Wed, 27 Nov 2024 17:22:30 +0100 |
On Wed, Nov 27, 2024 at 02:41:45PM +0100, lacsaP Patatetom wrote:
> Le mer. 27 nov. 2024 à 14:01, #!microsuxx <fxmbsw7@gmail.com> a écrit :
>
> > .. not spaces safe , but dev sys name usually doesnt have spaces
> >
> > On Wed, Nov 27, 2024, 1:58 PM #!microsuxx <fxmbsw7@gmail.com> wrote:
> >
> >> ls
> >> 2-3 2-3.1 2-3:1.0 usb2
> >> ~/tt $ a=( *-* ) a=( ${a[@]/*[:.]*} ) ; declare -p a
> >> declare -a a=([0]="2-3")
> >>
> >> On Wed, Nov 27, 2024, 12:56 PM lacsaP Patatetom <patatetom@gmail.com>
> >> wrote:
> >>
> >>> hi,
> >>>
> >>> I try to display only part of the `/sys/bus/usb/devices/` folder with `ls
> >>> /sys/bus/usb/devices/*-*[!.:]*` or `ls /sys/bus/usb/devices/*-*[^.:]*`
> >>> but
> >>> it doesn't subtract entries with a dot or colon in their name (only usb
> >>> entries are removed).
> >>>
> >>> if it's possible, what is the correct expression to use without having to
> >>> activate extended globbing ?
> >>>
> >>> regards, lacsaP.
> >>>
> >>> $ ls /sys/bus/usb/devices/
> >>> ...
> >>> /sys/bus/usb/devices/2-3
> >>> /sys/bus/usb/devices/2-3.1
> >>> /sys/bus/usb/devices/2-3:1.0
> >>> /sys/bus/usb/devices/usb2
> >>> ...
> >>>
> >>> $ ls /sys/bus/usb/devices/*-*[!.:]*
> >>> ...
> >>> /sys/bus/usb/devices/2-3
> >>> /sys/bus/usb/devices/2-3.1
> >>> /sys/bus/usb/devices/2-3:1.0
> >>> ...
> >>>
> >>
> this isn't quite what I want : with `ls` I'd like to list only those
> entries that match the regular expression
> `^/sys/bus/usb/devices/[0-9]+-[0-9]+$', but the bash expression
> `/sys/bus/usb/devices/*-*[!.:]*` used with `ls` doesn't return the desired
> entries.
> although the expression seems correct, it may not be possible with bash's
> default globbing...
>
> I use the `ls` command to address globbing, but in reality, the expression
> should take place in a `for` loop to replace this one which works as
> expected :
>
> $ for usb in $(find /sys/bus/usb/devices/ | grep -E '/[0-9]+-[0-9]+$'); do
> echo $usb; etc...; done
> /sys/bus/usb/devices/2-3
> /sys/bus/usb/devices/1-10
> /sys/bus/usb/devices/4-1
> /sys/bus/usb/devices/1-8
> /sys/bus/usb/devices/1-2
>
> when this one doesn't work :
>
> for usb in /sys/bus/usb/devices/*-*[!.:]*; do echo $usb; done
> /sys/bus/usb/devices/1-0:1.0
> /sys/bus/usb/devices/1-10
> /sys/bus/usb/devices/1-10:1.0
> /sys/bus/usb/devices/1-10:1.1
> ...
> /sys/bus/usb/devices/4-1.4
> /sys/bus/usb/devices/4-1.4:1.0
> /sys/bus/usb/devices/4-1:1.0
>
> the goal is to use only `bash` and abandon `find` and `grep` (`find` can do
> this on its own).
It seems to me that using an extended globbing pattern would be the
natural thing to do in this case, or you would not be using bash and
instead do it in POSIX sh.
shopt -s extglob nullglob
for dev in /dev/usb/devices/*-!(*[.:]*)
do
: stuff
done
If you're afraid to turn on the extglob option globally, you may
localise it to a subshell.
( shopt -s extglob nullglob
for ...as above...
done )
Anything else would just be inconvenient and awkward.
--
Andreas (Kusalananda) Kähäri
Uppsala, Sweden
.
Re: default/simple globbing, Tapani Tarvainen, 2024/11/27
Re: default/simple globbing, Chris Elvidge, 2024/11/28