public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] Question about handling filenames with "illegal" characters...
@ 2020-04-16 15:15 tuxic
  2020-04-16 15:19 ` Neil Bothwick
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: tuxic @ 2020-04-16 15:15 UTC (permalink / raw
  To: Gentoo

Hi,

a loop like this

for fn in asd* ; do
    do_something $fn
done

fails, when a file is named like this:

    List of OSses allowing spaces in filenames.txt

.

Normally I would replace the asd* with the according

find . -name 'asd*' -print0 | <more here>

but I got in trpuble, because "do_something" 
now misunderstood the whole thing.

Is there a way to express $fn in a way, so that
do_something get one filename at a time and
whole thing does not is torn apart by some
not so nice filenames?

Cheers!
Meino





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [gentoo-user] Question about handling filenames with "illegal" characters...
  2020-04-16 15:15 [gentoo-user] Question about handling filenames with "illegal" characters tuxic
@ 2020-04-16 15:19 ` Neil Bothwick
  2020-04-16 15:26   ` tuxic
  2020-04-16 15:29   ` Rich Freeman
  2020-04-16 15:21 ` Francesco Turco
  2020-04-16 16:31 ` Michael Orlitzky
  2 siblings, 2 replies; 9+ messages in thread
From: Neil Bothwick @ 2020-04-16 15:19 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 367 bytes --]

On Thu, 16 Apr 2020 17:15:45 +0200, tuxic@posteo.de wrote:

> a loop like this
> 
> for fn in asd* ; do
>     do_something $fn
> done
> 
> fails, when a file is named like this:
> 
>     List of OSses allowing spaces in filenames.txt
> 

do_something "$fn"

Would this work?


-- 
Neil Bothwick

C:\BELFRY is where I keep my .BAT files ^^^oo^^^

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [gentoo-user] Question about handling filenames with "illegal" characters...
  2020-04-16 15:15 [gentoo-user] Question about handling filenames with "illegal" characters tuxic
  2020-04-16 15:19 ` Neil Bothwick
@ 2020-04-16 15:21 ` Francesco Turco
  2020-04-16 16:16   ` tuxic
  2020-04-16 16:31 ` Michael Orlitzky
  2 siblings, 1 reply; 9+ messages in thread
From: Francesco Turco @ 2020-04-16 15:21 UTC (permalink / raw
  To: gentoo-user

On Thu, Apr 16, 2020, at 17:15, tuxic@posteo.de wrote:
> Normally I would replace the asd* with the according
> 
> find . -name 'asd*' -print0 | <more here>
> 
> but I got in trpuble, because "do_something" 
> now misunderstood the whole thing.

What about the following command?

find . -name 'asd*' -print0 | xargs -0 <more here>

-- 
https://fturco.net/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [gentoo-user] Question about handling filenames with "illegal" characters...
  2020-04-16 15:19 ` Neil Bothwick
@ 2020-04-16 15:26   ` tuxic
  2020-04-16 15:29   ` Rich Freeman
  1 sibling, 0 replies; 9+ messages in thread
From: tuxic @ 2020-04-16 15:26 UTC (permalink / raw
  To: gentoo-user

On 04/16 04:19, Neil Bothwick wrote:
> On Thu, 16 Apr 2020 17:15:45 +0200, tuxic@posteo.de wrote:
> 
> > a loop like this
> > 
> > for fn in asd* ; do
> >     do_something $fn
> > done
> > 
> > fails, when a file is named like this:
> > 
> >     List of OSses allowing spaces in filenames.txt
> > 
> 
> do_something "$fn"
> 
> Would this work?
> 
> 
> -- 
> Neil Bothwick
> 
> C:\BELFRY is where I keep my .BAT files ^^^oo^^^


Hi Neil,

is this the "Bothwick effect" ?

This was the first I tried after my script failed...and it failed
again after adding the "".
I tried it again...and now it works. 
Only difference: I did it from the commandline (using zsh).

I am baffled.

But it is better, I don't completely understand, whu it is working
now than vice versa ;)

Cheers!
Meino





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [gentoo-user] Question about handling filenames with "illegal" characters...
  2020-04-16 15:19 ` Neil Bothwick
  2020-04-16 15:26   ` tuxic
@ 2020-04-16 15:29   ` Rich Freeman
  2020-04-16 16:15     ` tuxic
  1 sibling, 1 reply; 9+ messages in thread
From: Rich Freeman @ 2020-04-16 15:29 UTC (permalink / raw
  To: gentoo-user

On Thu, Apr 16, 2020 at 11:19 AM Neil Bothwick <neil@digimed.co.uk> wrote:
>
> On Thu, 16 Apr 2020 17:15:45 +0200, tuxic@posteo.de wrote:
>
> > a loop like this
> >
> > for fn in asd* ; do
> >     do_something $fn
> > done
> >
> > fails, when a file is named like this:
> >
> >     List of OSses allowing spaces in filenames.txt
> >
>
> do_something "$fn"
>

That, or use ${fn} - that might be more resistant to quotes in
filenames though I haven't looked closely into that.

If you look at most ebuilds they're full of quoted or braced variables
for exactly this reason.  If you want to stick spaces in your
directory paths that is your mess to deal with, but Gentoo won't
break.

On a side note, if you have a directory full of crufty filenames the
detox program is very useful for cleaning them up.  Of course, that is
invasive and you probably don't want to rely on that in your script.

-- 
Rich


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [gentoo-user] Question about handling filenames with "illegal" characters...
  2020-04-16 15:29   ` Rich Freeman
@ 2020-04-16 16:15     ` tuxic
  0 siblings, 0 replies; 9+ messages in thread
From: tuxic @ 2020-04-16 16:15 UTC (permalink / raw
  To: gentoo-user

On 04/16 11:29, Rich Freeman wrote:
> On Thu, Apr 16, 2020 at 11:19 AM Neil Bothwick <neil@digimed.co.uk> wrote:
> >
> > On Thu, 16 Apr 2020 17:15:45 +0200, tuxic@posteo.de wrote:
> >
> > > a loop like this
> > >
> > > for fn in asd* ; do
> > >     do_something $fn
> > > done
> > >
> > > fails, when a file is named like this:
> > >
> > >     List of OSses allowing spaces in filenames.txt
> > >
> >
> > do_something "$fn"
> >
> 
> That, or use ${fn} - that might be more resistant to quotes in
> filenames though I haven't looked closely into that.
> 
> If you look at most ebuilds they're full of quoted or braced variables
> for exactly this reason.  If you want to stick spaces in your
> directory paths that is your mess to deal with, but Gentoo won't
> break.
> 
> On a side note, if you have a directory full of crufty filenames the
> detox program is very useful for cleaning them up.  Of course, that is
> invasive and you probably don't want to rely on that in your script.
> 
> -- 
> Rich
> 

Hi Rich,

these [CENSORED] filenames are mainly copies from the contents of
usbstick, which were previoysly filled using machines, which were
neither UNICES nor Linuxxes nor MacOSxxes ... you know what I mean.
I myself stick to sane filenames. 

Detox is known to me. But for certain reasons I cannot change the
filenames now...I have to process them first.

Cheers!
Meino




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [gentoo-user] Question about handling filenames with "illegal" characters...
  2020-04-16 15:21 ` Francesco Turco
@ 2020-04-16 16:16   ` tuxic
  0 siblings, 0 replies; 9+ messages in thread
From: tuxic @ 2020-04-16 16:16 UTC (permalink / raw
  To: gentoo-user

On 04/16 05:21, Francesco Turco wrote:
> On Thu, Apr 16, 2020, at 17:15, tuxic@posteo.de wrote:
> > Normally I would replace the asd* with the according
> > 
> > find . -name 'asd*' -print0 | <more here>
> > 
> > but I got in trpuble, because "do_something" 
> > now misunderstood the whole thing.
> 
> What about the following command?
> 
> find . -name 'asd*' -print0 | xargs -0 <more here>
> 
> -- 
> https://fturco.net/
> 

I would do that normally, but the <more> to much "more"....




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [gentoo-user] Question about handling filenames with "illegal" characters...
  2020-04-16 15:15 [gentoo-user] Question about handling filenames with "illegal" characters tuxic
  2020-04-16 15:19 ` Neil Bothwick
  2020-04-16 15:21 ` Francesco Turco
@ 2020-04-16 16:31 ` Michael Orlitzky
  2020-04-16 21:15   ` [gentoo-user] " Ian Zimmerman
  2 siblings, 1 reply; 9+ messages in thread
From: Michael Orlitzky @ 2020-04-16 16:31 UTC (permalink / raw
  To: gentoo-user

On 4/16/20 11:15 AM, tuxic@posteo.de wrote:
> 
> Is there a way to express $fn in a way, so that
> do_something get one filename at a time and
> whole thing does not is torn apart by some
> not so nice filenames?
> 

What are your constraints... are you using bash, or just any POSIX
shell? Can you rely on GNU extensions to find/xargs, etc? Is
do_something a shell command or a program?

Even with bash, find/xargs tends to fall down if you need to run a
series of shell commands on each thing found. The simplest way to handle
that is to use "while read..." with the bash-specific null separator to
loop through the files one at a time, like in

https://gitweb.gentoo.org/proj/portage.git/tree/bin/install-qa-check.d/90bad-bin-owner

If you want it to be portable, on the other hand, I recently wasted
several hours on this problem and it's not pretty. Something like,

  find -name 'whatever' \
    -exec sh -c "
      for f in \"\${@}\"; do
        do_stuff \"\${f}\" && echo \"\${f}\"
      done
    " - {} +

will do it.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [gentoo-user] Re: Question about handling filenames with "illegal" characters...
  2020-04-16 16:31 ` Michael Orlitzky
@ 2020-04-16 21:15   ` Ian Zimmerman
  0 siblings, 0 replies; 9+ messages in thread
From: Ian Zimmerman @ 2020-04-16 21:15 UTC (permalink / raw
  To: gentoo-user

On 2020-04-16 12:31, Michael Orlitzky wrote:

>   find -name 'whatever' \
>     -exec sh -c "
>       for f in \"\${@}\"; do
>         do_stuff \"\${f}\" && echo \"\${f}\"
>       done
>     " - {} +

# untested, use at own risk
NL='
'
export NL
AWKPROG='{print "do_stuff @" $0 "@ && printf %s @" $0 "@ :$NL:";}'
AWKPROG=${AWKPROG//@/"'"} ; AWKPROG=${AWKPROG//:/'"'}
find -name "$FOO" | awk "$AWKPROG" | sh

This should work even if sh is dash - only the AWKPROG translations are
bash specific (and they could probably be avoided at the cost of other
ugliness, maybe with sed).

BTW, the only difference between $FOO and ${FOO} is to protect against
alphanumerics literally following the occurence of FOO.  It has no
quoting effect on the _contents_ of $FOO.

-- 
Ian


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-04-16 21:15 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-16 15:15 [gentoo-user] Question about handling filenames with "illegal" characters tuxic
2020-04-16 15:19 ` Neil Bothwick
2020-04-16 15:26   ` tuxic
2020-04-16 15:29   ` Rich Freeman
2020-04-16 16:15     ` tuxic
2020-04-16 15:21 ` Francesco Turco
2020-04-16 16:16   ` tuxic
2020-04-16 16:31 ` Michael Orlitzky
2020-04-16 21:15   ` [gentoo-user] " Ian Zimmerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox