How can I parse filenames read from STDIN via the standard Unix parsing
rules (without duplicating said rules)? All of the following would be
parsed as coherent file names:
file.txt
my\ file.txt
“my file.txt”
A nice addition would be that *.txt would use Bash expansion to find the
appropriate files.
ARGF isn’t working for me because it tries to actually read the files in
(and some of the arguments in STDIN are commands, not filenames).
How can I parse filenames read from STDIN via the standard Unix parsing rules
(without duplicating said rules)? All of the following would be parsed as coherent
file names:
file.txt
my\ file.txt
“my file.txt”
A nice addition would be that *.txt would use Bash expansion to find the
appropriate files.
Use Shellwords to handle shell escaped strings and Dir[] to expand
wildcards:
require ‘shellwords’
while line = STDIN.gets
filename = Shellwords.shellwords(line).join(‘’)
p filename
One can as well use the usual iteration for reading lines from files.
=>
require ‘shellwords’
$stdin.each_line do |line|
filename = Shellwords.shellwords(line).join(‘’)
p filename
expand wildcards
p Dir[filename]
end
Advantage is also that the scope of “line” is limited to the block
reducing potential for errors.
Btw, Sean, are you sure David will join all the shellwords? I am
asking since he said there were commands as well and he basically
wants to parse in the same way the shell does. So: