0

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
intechops6
  • 1,007
  • 4
  • 22
  • 43

2 Answers2

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.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • Why do you need four `'` at the start of the line, but only three at the end? Is there something I am missing? – FlyingTeller Feb 01 '21 at 08:40
  • @FlyingTeller the four `'` 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. – Ferenc Deak Feb 01 '21 at 09:37
0

it's just for developers to note it has combination of shell and python, they are running it as a python file

  • 1
    This is pretty confusing, I'd create a .sh file and make it call the .py file there. Also a post that explains the bash bit: https://stackoverflow.com/q/52785232/9824103 – sleepystar96 Feb 01 '21 at 08:26
  • @hecohaf944 - The line 2 has 4 single quotes at the beginning and 3 single quotes at the end (''''exec python -u -- "$0" ${1+"$@"} # '''). is this the way to include python code in Shell script? – intechops6 Feb 01 '21 at 08:31
  • What makes you so certain that it is run as a python file? Is there any evidence for that? – FlyingTeller Feb 01 '21 at 08:39
  • 1
    @intechops6 yes, you can try it out in your bash terminal: `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. – sleepystar96 Feb 01 '21 at 08:49
  • @sleepystar96 got it now. reading the filename as $0 and arguments inside the script. will execute in the terminal to understand it better. – intechops6 Feb 01 '21 at 15:00