help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Evaluations of backticks in if statements


From: Andy Chu
Subject: Re: [Help-bash] Evaluations of backticks in if statements
Date: Thu, 23 Feb 2017 09:55:59 -0800

Sorry for so many messages, but I remembered that this relates to a curious
idiom I found while parsing bash scripts "in the wild" (example:
http://www.oilshell.org/blog/2016/11/09.html ):

In this script:

https://github.com/sandstorm-io/sandstorm/blob/master/install.sh

The `# comment` hack takes advantage of the fact that empty unquoted words
are elided.  If you quoted them then your argv array would have '' all over
the place and the command wouldn't work.

And comments aren't a lexical construct; they can appear INSIDE a
subshell.  Unlike in other languages, a comment is more naturally handled
at the parsing level rather than the lexing level, because say

$ echo foo#bar
foo#bar

doesn't require quotes.



# Generate key for client certificate. OpenSSL will read from
# /dev/urandom by default, so this won't block. We abuse the ``
# operator so we can have inline comments in a multi-line command.
openssl \
req `# Invoke OpenSSL's PKCS#10 X.509 bits.` \
-new `# Create a new certificate/request.` \
-newkey rsa:4096 `# Create a new RSA key of length 4096 bits.` \
-days 3650 `# Make the self-signed cert valid for 10 years.` \
-nodes `# no DES -- that is, do not encrypt the key at rest.` \
-x509 `# Output a certificate, rather than a signing request.` \
`# Sandcats ignores the subject in the certificate; use` \
`# OpenSSL defaults.` \
-subj "/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd" \
-keyout var/sandcats/id_rsa `# Store the resulting RSA private key in id_rsa
` \
-out var/sandcats/id_rsa.pub `# Store the resulting certificate in
id_rsa.pub` \
2>/dev/null `# Silence the progress output.`






On Thu, Feb 23, 2017 at 9:45 AM, Andy Chu <address@hidden> wrote:

> OK, there is no special case.  This is because of what I think of as
> "empty unquoted word elision" (not sure if there is a better name).
>
> `true` is not quoted so it doesn't result in an empty string in argv, it
> results in NOTHING in argv (empty argv).
>
> Compare:
>
> $ if `true`; then echo TRUE; else echo FALSE; fi
> TRUE
>
> $ if "`true`"; then echo TRUE; else echo FALSE; fi
> : command not found
> FALSE
>
> # passing argument -- ZZZ is the command name, not empty string
> $ if `true` ZZZ; then echo TRUE; else echo FALSE; fi
> ZZZ: command not found
> FALSE
>
> # now it has output, but this isn't a special case either
> $ if `sh -c 'echo YYY; true'`; then echo TRUE; else echo FALSE; fi
> YYY: command not found
> FALSE
>
> This is just like how variables are elided if empty and quoted:
>
> $ empty=; python -c 'import sys; print sys.argv[1:]' a $empty b
> ['a', 'b']
>
> $ empty=; python -c 'import sys; print sys.argv[1:]' a "$empty" b
> ['a', '', 'b']
>
>
> Andy
>
>
>


reply via email to

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