help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't find file


From: Eli Schwartz
Subject: Re: Can't find file
Date: Sat, 9 Nov 2019 20:13:25 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2

On 11/9/19 7:39 PM, David Niklas wrote:
> Hello,
> 
> ls -- $( awk -v z="../EXAMPLE FILE NAME" 'BEGIN{ print z }' )
> 
> I've tried single and double quotes in the print part of the awk 1 liner
> without success.

I'm baffled at what you're trying to do here, but...

This:
awk -v z="../EXAMPLE FILE NAME" 'BEGIN{ print z }'

is doing two things. First, it is defining an awk variable z, which
equals the string "../EXAMPLE FILE NAME". Then, it is running an awk
process that accepts no input, and immediately prints the text from that
variable.

Then, this output string from awk (which is "../EXAMPLE FILE NAME") is
interpolated onto the command line, and the following is run:

ls -- ../EXAMPLE FILE NAME

This is going to work totally fine... as long as you intend ls to
operate on three files:

../EXAMPLE

FILE

NAME

> I've also tried double quoting the whole thing without any change.

You've tried double-quoting what? Your $(awk ...) command substitution?
Clearly not, because if you had used

ls -- "$(awk ...)"

Then you would have ended up with the final parsed command line being

ls -- "quoted-output-of-awk"


or in this case,

ls -- "../EXAMPLE FILE NAME"

> I've no idea what's going on. The file does exist. You can ls it without
> the awk 1 liner.

You cannot, in fact, ls it without the awk one-liner, or at least, you
cannot do so without understanding how a bash shell works. Quotes are
important. Knowing where quotes go is important too.

So, now I have a question for you. When you say, "I've tried it without
success", this is useless information. We don't know what success means,
and we don't know what your error is either. In this case, the error
seems pretty easy to guess, and therefore, I have guessed. But I still
don't know where you tried to put quotes, and I don't know what error
message you got when you tried *that*.

Did you get this error message?

$ ls -- $( awk -v z="../EXAMPLE FILE NAME" 'BEGIN{ print z }' )
ls: cannot access '../EXAMPLE': No such file or directory
ls: cannot access 'FILE': No such file or directory
ls: cannot access 'NAME': No such file or directory

(Oops, no quotes, this is treated as three different files.)

Or what about this, which you claim you have also tried, and it does not
work:

$ ls -- "$( awk -v z="../EXAMPLE FILE NAME" 'BEGIN{ print z }' )"
ls: cannot access '../EXAMPLE FILE NAME': No such file or directory

(I do not have a file named '../EXAMPLE FILE NAME', but you say you do,
so this should work for you albeit not for me. It *should*.)

...

Finally, what are you actually trying to do. I'm guessing not to ls a
file by using awk. Does the actual case which you are really trying to
accomplish, but are most likely not asking about, require you to
tokenize multiple arguments to the command that isn't ls? Because I
would not recommend quoting your command substitution "$(awk ...)" and
using it in other commands, if so. Other possible solutions include:

awk ... | while IFS= read -r line; do
    somecommand -- "$line"
done

if you can guarantee that everything will be newline-delimited when
coming out of awk.

You can also use awk's builtin system() command:

awk -v z="../EXAMPLE FILE NAME" 'BEGIN{ system("somecommand -- \"" z
"\"" ) }'

But quoting gets annoying, and awk is not exactly a great language for
passing filenames as arguments and immediately system()ing out to
another command, plus, filenames with embedded " characters will eat
your awk system() for lunch, even if we've successfully evaded space
wordsplitting issues.

$ awk -v z="../EXAMPLE FILE\" NAME" 'BEGIN{ system("ls -- \"" z "\"" ) }'
sh: 1: Syntax error: Unterminated quoted string


So basically it would really help if we understood what you were trying
to do.

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]