I came across this python(.py) file but it has SHELL interpreter in the first line but subsequent lines are python code in it.
It's clear that it is python file, but why is the first line has SHELL shebang in it.
If this is a SHELL script, why is the file has the extentsion .py
If this is SHELL script, how does the below code interpreted by SHELL.
#!/bin/sh
''''exec python -u -- "0ドル" ${1+"$@"} # '''
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import os
2 Answers 2
That's done like, look at the following more expanded version of the script:
#!/bin/sh
''''echo 0ドル "is called. Hello shell world: PARS:" ${1+"$@"} #'''
''''exec python -u -- "0ドル" ${1+"$@"} # '''
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import os
import sys
print "Hello python world", sys.argv
Shrtoly: Firstly the shell takes over the first few lines and then calls the python interpreter with itself.
The stuff after '''' is ignored by the python interpeter as being a multiline comment: Is there a way to create multiline comments in Python? so they are picked up by the shell instead, and the shell gives over the control to the python interpreter after the line ''''exec python -u -- "0ドル" ${1+"$@"} # ''' so the python picks up the script that comes in in 0ドル (which now we have printed) and with all its arguments as per ${1+"$@"} that gives a list of the arguments passed in.
From this point on it's up to python to do whatever he wants with the script.
2 Comments
' at the start of the line, but only three at the end? Is there something I am missing?' will evaulate to two '' ie. two empty strings, that will be joined with the echo, thus giving nothing before echo (try to run ''''echo ABC and 'X'echo ABC and ''echo ABC, see which works and which does not). If you would have three that would be an empty string '' and another string: 'echo .... . The part which is after the # is ignored by the shell script, since it's a comment, so the bash does not see the ''' at the end.it's just for developers to note it has combination of shell and python, they are running it as a python file
5 Comments
exec python -u "filename". Your like uses 0,ドル which willl replace filename. Everything that comes after 0ドル are arguments, in this case, nothing. Python will also think that the shell line is a comment.