I have a need� to get the full path of a file that has spaces in its name to use as a program argument
e.g.
jeremy@client:~$ ls -l name\ with\ spaces
-rw-r--r-- 1 jeremy jeremy 0 May� 3 06:51 'name with spaces'
jeremy@client:~$ realpath name\ with\ spaces
/home/jeremy/name with spaces
The spaces without quotes cause problems with subsequent processing.
Can realpath or other utility return a quoted pathname?
On 3/5/24 07:29, Greg Wooledge wrote:
The spaces without quotes cause problems with subsequent processing.Then the subsequent processing has bugs in it. Fix them.
Can realpath or other utility return a quoted pathname?That would be extremely counterproductive. Do not look for kludges to
work around your script's bugs. Fix the bugs instead.
Start with<https://mywiki.wooledge.org/Quotes>.
You don't see a problem that ls produces quoted filenames and realpath doesn't?
I have a need to get the full path of a file that has spaces in its
name to use as a program argument
e.g.
jeremy@client:~$ ls -l name\ with\ spaces
-rw-r--r-- 1 jeremy jeremy 0 May 3 06:51 'name with spaces' jeremy@client:~$ realpath name\ with\ spaces
/home/jeremy/name with spaces
The spaces without quotes cause problems with subsequent processing.
Can realpath or other utility return a quoted pathname?
Perhaps Perl and the module String::ShellQuote ?
2024-05-02 18:50:28 dpchrist@laalaa ~
$ touch "name with spaces"
2024-05-02 18:50:45 dpchrist@laalaa ~
$ touch "name with\nnewline"
2024-05-02 19:06:01 dpchrist@laalaa ~
$ perl -MString::ShellQuote -e 'print shell_quote(@ARGV), "\n"' name*
'name with spaces' 'name with\nnewline'
I have a need� to get the full path of a file that has spaces in its
name to use as a program argument
jeremy@client:~$ realpath name\ with\ spaces
/home/jeremy/name with spaces
Can realpath or other utility return a quoted pathname?
On Thu, May 02, 2024 at 07:11:46PM -0700, David Christensen wrote:
Perhaps Perl and the module String::ShellQuote ?
2024-05-02 18:50:28 dpchrist@laalaa ~
$ touch "name with spaces"
2024-05-02 18:50:45 dpchrist@laalaa ~
$ touch "name with\nnewline"
You didn't create a name with a newline in it here. You created a name
with a backslash in it. If you wanted a newline, you would have to use
the $'...' quoting form (in bash).
touch $'name with\nnewline'
bar"
bar'
I still insist that this is a workaround that should *not* be used
to try to cancel out quoting bugs in one's shell scripts. Just write
the shell scripts correctly in the first place.
On 03/05/2024 09:19, Greg Wooledge wrote:
I still insist that this is a workaround that should *not* be used
to try to cancel out quoting bugs in one's shell scripts.
There are still specific cases when quoting is necessary, e.g. ssh
remote command
(however you have to be sure concerning shell on the
remote host).
In BASH printf has %q format. GNU coreutils supports it as well, but
dash does not, so be careful.
Likely Jeremy's case does not really require this kind of quoting.
While "ls -l" output is for humans, realpath is often used in scripts. Certainly it should nor return quoted output by default. I am in doubts
if a dedicated option should be added to realpath.
newline'
I am unable to find $'string' in the dash(1) man page (?). As I typically write "#!/bin/sh" shell scripts, writing such to deal with file names containing non-printing characters is going to baffle me.
My use case is very simple. Give an argument to a program that expects a single filename/path.
If you give it an unquoted and unescaped filename it will break parsing the args thinking there are many.
When invoking from bash with auto completion the filename will get escaped
as required. When cutting and pasting into a debugger prompt for args, not so.
On 3/5/24 19:06, Greg Wooledge wrote:
I would suggest that if you need to use a debugger to track down a bug
in your program, you should use filenames that don't require quoting
when you set up your tests.
1970's style static test cases are not relevant here.
In the real world...� I download files generated by another system that
are constantly changing content and with names I don't control.
My workflow is to download a new file from a remote source and then run my processor over it.
As a necessary consequence I need the fully quoted or escaped file name of the new file to feed to the processor/debugger.
I can obviously add an extra step to the process to convert the new file
name to something acceptable before processing. However, my question was
how to avoid that extra step by getting fully quoted filenames to process.
On Thu, May 02, 2024 at 10:18:03PM -0700, David Christensen wrote:
I am unable to find $'string' in the dash(1) man page (?). As I typically >> write "#!/bin/sh" shell scripts, writing such to deal with file names
containing non-printing characters is going to baffle me.
Currently, $' quoting is a bash extension. It's supposed to appear in
some future edition of POSIX, at which point shells like dash will be required to adopt it (whenever they get around to it). For now, though,
you should consider it bash only.
On 3/5/24 19:06, Greg Wooledge wrote:
I would suggest that if you need to use a debugger to track down a bug
in your program, you should use filenames that don't require quoting
when you set up your tests.
1970's style static test cases are not relevant here.
In the real world... I download files generated by another system that
are constantly changing content and with names I don't control.
My workflow is to download a new file from a remote source and then run
my processor over it.
As a necessary consequence I need the fully quoted or escaped file name
of the new file to feed to the processor/debugger.
I can obviously add an extra step to the process to convert the new file
name to something acceptable before processing. However, my question was
how to avoid that extra step by getting fully quoted filenames to process.
$ 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.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (0 / 16) |
| Uptime: | 162:15:53 |
| Calls: | 12,094 |
| Calls today: | 2 |
| Files: | 15,000 |
| Messages: | 6,517,780 |