Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #269192

Re: realpath quoting

From Greg Wooledge <greg@wooledge.org>
Newsgroups linux.debian.user
Subject Re: realpath quoting
Date 2024-05-04 16:00 +0200
Message-ID <IAo5I-b2rt-5@gated-at.bofh.it> (permalink)
References (1 earlier) <IzQGJ-aHiZ-1@gated-at.bofh.it> <IzQGJ-aHiZ-3@gated-at.bofh.it> <IzRjr-aHve-1@gated-at.bofh.it> <IzTOi-aJfj-13@gated-at.bofh.it> <IAnCF-b2ht-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Sat, May 04, 2024 at 08:22:27AM -0500, Tom Browder wrote:
> $ cat read.raku
> #!/usr/bin/env raku
> my $a = "name with spaces";
> my $b = "name\nwith newline";
> say "file 1: |$a|";
> say "file 2: |$b|";
> 
> And executing it:
> 
> $ ./read.raku
> file 1: |name with spaces|
> file 2: |name
> with newlines|
> 
> With Raku, it's easy to search the directory for the weird file names,
> open them, and use their contents.

You've not really demonstrated anything that can't be done in every other
scripting language.

hobbit:~$ cat foo
#!/bin/bash
a='name with spaces'
b=$'name\nwith newline'
printf 'file 1: |%s|\n' "$a"
printf 'file 1: |%s|\n' "$b"
hobbit:~$ ./foo
file 1: |name with spaces|
file 1: |name
with newline|

hobbit:~$ cat bar
#!/usr/bin/tclsh8.6
set a "name with spaces"
set b "name\nwith newline"
puts "file 1: |$a|"
puts "file 2: |$b|"
hobbit:~$ ./bar
file 1: |name with spaces|
file 2: |name
with newline|

hobbit:~$ cat baz
#!/bin/sh
a='name with spaces'
b='name
with newline'
printf 'file 1: |%s|\n' "$a"
printf 'file 2: |%s|\n' "$b"
hobbit:~$ ./baz
file 1: |name with spaces|
file 2: |name
with newline|


The only part of this that's even *slightly* awkward is loading a literal
newline into a variable in /bin/sh.  And that part drops away and ceases
to be a problem when you read the filename from some kind of input
source (such as a directory).

In real life:

hobbit:~$ mkdir /tmp/x && cd /tmp/x
hobbit:/tmp/x$ touch 'name with spaces' $'name\nwith newline'
hobbit:/tmp/x$ vi foo
hobbit:/tmp/x$ chmod +x foo
hobbit:/tmp/x$ cat foo
#!/bin/sh
for f in *; do
    printf 'Next file: |%s|\n' "$f"
done
hobbit:/tmp/x$ ./foo
Next file: |foo|
Next file: |name
with newline|
Next file: |name with spaces|

There's nothing in here that requires an advanced language.  /bin/sh can
do it all perfectly well.  In fact, we haven't even reached the limits
of what /bin/sh can do yet.

hobbit:/tmp/x$ vi foo
hobbit:/tmp/x$ cat foo
#!/bin/sh
printf 'Next file: |%s|\n' *
hobbit:/tmp/x$ ./foo
Next file: |foo|
Next file: |name
with newline|
Next file: |name with spaces|

Is that useful in real life?  Maybe.  Maybe not.  But it's available.

Correct use of quotes and globs solves most of the problems that people
have with sh.

Can it solve "I have to manually paste filenames containing spaces and
punctuation out of a spreadsheet into a shell"?  No, probably not.
But then, what can?  Sometimes, the workflow is what has to change.

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

realpath quoting jeremy ardley <jeremy.ardley@gmail.com> - 2024-05-03 01:10 +0200
  Re: realpath quoting Greg Wooledge <greg@wooledge.org> - 2024-05-03 01:40 +0200
    Re: realpath quoting Greg Wooledge <greg@wooledge.org> - 2024-05-03 01:50 +0200
    Re: realpath quoting jeremy ardley <jeremy.ardley@gmail.com> - 2024-05-03 01:50 +0200
  Re: realpath quoting David Christensen <dpchrist@holgerdanske.com> - 2024-05-03 04:20 +0200
    Re: realpath quoting Greg Wooledge <greg@wooledge.org> - 2024-05-03 04:20 +0200
      Re: realpath quoting Max Nikulin <manikulin@gmail.com> - 2024-05-03 05:00 +0200
        Re: realpath quoting jeremy ardley <jeremy.ardley@gmail.com> - 2024-05-03 06:40 +0200
          Re: realpath quoting Greg Wooledge <greg@wooledge.org> - 2024-05-03 13:10 +0200
            Re: realpath quoting jeremy ardley <jeremy.ardley@gmail.com> - 2024-05-03 13:40 +0200
              Re: realpath quoting Sirius <sirius@trudheim.com> - 2024-05-03 13:50 +0200
              Re: realpath quoting David Christensen <dpchrist@holgerdanske.com> - 2024-05-03 21:20 +0200
          Re: realpath quoting Max Nikulin <manikulin@gmail.com> - 2024-05-03 17:20 +0200
        Re: realpath quoting David Christensen <dpchrist@holgerdanske.com> - 2024-05-03 07:40 +0200
          Re: realpath quoting Tom Browder <tom.browder@gmail.com> - 2024-05-04 15:30 +0200
            Re: realpath quoting Greg Wooledge <greg@wooledge.org> - 2024-05-04 16:00 +0200
      Re: realpath quoting David Christensen <dpchrist@holgerdanske.com> - 2024-05-03 07:20 +0200
        Re: realpath quoting Greg Wooledge <greg@wooledge.org> - 2024-05-03 13:10 +0200
          Re: realpath quoting David Christensen <dpchrist@holgerdanske.com> - 2024-05-03 20:50 +0200
  Re: realpath quoting Teemu Likonen <tlikonen@iki.fi> - 2024-05-03 06:50 +0200

csiph-web