uses both shell and python codes in one script.

Chris Angelico rosuav at gmail.com
Thu Oct 3 19:15:41 EDT 2019


On Fri, Oct 4, 2019 at 9:08 AM Cameron Simpson <cs at cskk.id.au> wrote:
>
> On 03Oct2019 23:55, Chris Angelico <rosuav at gmail.com> wrote:
> >On Thu, Oct 3, 2019 at 11:41 PM Hongyi Zhao <hongyi.zhao at gmail.com> wrote:
> >> I'm very confusing on the following part in this script:
> >>
> >> ----
> >> ''':' # begin python string; this line is interpreted by the shell as `:`
> >> which python  >/dev/null 2>&1 && exec python  "$0" "$@"
> >> which python3 >/dev/null 2>&1 && exec python3 "$0" "$@"
> >> which python2 >/dev/null 2>&1 && exec python2 "$0" "$@"
> >> >&2 echo "error: cannot find python"
> >> exit 1
> >> '''
> >> ----
> >>
> >> Any hints for the meaning of several ' used above?
> >
> >The hint is there in that line, and stems from the way two different
> >parsers (Python and sh) interpret the line. [...]
> >
> >In the shell, the first two are an
> >empty string, then ':' is a colon, which introduces a label (the fact
> >that it's in quotes is irrelevant to the shell). So there's an empty
> >label followed by a shell comment.
>
> No. ":" is a synonym for "true". (Like "[" is a synonym for "test".)
>
> There aren't labels in the shell. This is just a dummy "true" command,
> with a comment.

My bad. I remember using a leading colon-space as a multi-shell
comment, and in at least some of them, it's a label (probably old
MS-DOS batch files or something, given what my background is). In any
case, that one line does nothing, but the subsequent lines are
meaningful.

ChrisA



More information about the Python-list mailing list