I'm not sure what Python shell=True is doing

Asked 2 years ago, Updated 2 years ago, 20 views

I'm working on a shell script using Python (Python 2.6.x version).

The subprocess package has been used. I searched and found that some people use shell=True and others don't. I don't see the difference.

subprocess.call("date", shell=True)
subprocess.call("date")

has the same result value. I don't know what you're doing.

python

2022-09-22 11:13

1 Answers

There is a difference when handing over the factor to the Byeongryeong word.

Running with shell=True usually runs without a separate validation, as shell commands are issued, making it vulnerable to shell injection

If you add the factor to the command as shown below, you can check the difference.

For shell = True

>>> call("ls /", shell = True)
Applications                    System                          Volumes                         dev                             installer.failurerequests       private                         usr
Library                         TMVersion.ini                   bin                             etc                             net                             sbin                            var
Network                         Users                           cores                           home                            opt                             tmp
0

For shell = False

>>> call("ls /", shell = False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 523, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1343, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

More detailed descriptions are provided in the document below.

https://docs.python.org/2/library/subprocess.html


2022-09-22 11:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.