Skip to main content

Using the Shell


Let's start with a simple example and run the Hello World! example in the same way as before, passing the command though the shell:

>>> import subprocess
>>> subprocess.Popen('echo "Hello world!"', shell=True)
Hello world!


As you can see, this prints Hello world! to the standard output as before but the interactive console also displays that we have created an instance of the subprocess.Popen class.

If you save this as process_test.py and execute it on the command line you get the same result:

$ python process_test.py
Hello world!

So far so good.

You might we wondering which shell is being used. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable. When you specify shell=True you can customise the shell to use with the executable argument.

>>> subprocess.Popen('echo "Hello world!"', shell=True, executable="/bin/bash")
Hello world!


This example works the same as before but if you were using some shell-specific features you would notice the differences.

Let's explore some other features of using the shell:

Variable expansion:

>>> subprocess.Popen('echo $PWD', shell=True)
/home/james/Desktop


Pipes and redirects:

subprocess.Popen('echo "Hello world!" | tr a-z A-Z 2> errors.txt', shell=True)

>>> HELLO WORLD!

The errors.txt file will be empty because there weren't any errors. Interstingly on my computer, the Popen instance is displayed before the HELLO WORLD! message is printed to the standard output this time. Pipes and redirects clearly work anyway.

Here documents:

>>> subprocess.Popen("""
... cat << EOF > new.txt
... Hello World!
... EOF
... """, shell=True)


The new.txt file now exists with the content Hello World!.

Unsurprisingly the features that work with a command passed to the shell via the command line also work when passed to the shell from Python, including shell commands.

Comments

Popular posts from this blog

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checkin...