0

How do i call a unix command such as df -Ph | awk 'NR>=2 {print $6","$5","$4}' using subprocess. Would it make sense to use shlex.split here?

Thanks for any assistance here.

3 Answers 3

1

You're using a pipe, so it needs to run in the shell. So just use the string form and make sure to specify shell=True. As for the quoting, it's easiest to use a triple quote here:

cmd = """df -Ph | awk 'NR>=2 {print $6","$5","$4}'"""
Sign up to request clarification or add additional context in comments.

Comments

1

Just have subprocess pass it to a shell by setting shell=True:

subprocess.call('''df -Ph | awk 'NR>=2 {print $6","$5","$4}'''', shell=True)

1 Comment

Worked with the triple double quotes - as shown in Kindalls solution - instead of the single. Thanks for the response.
0

Hi you can also do like this. Do not forget to import sub-process

import subprocess
def linuxOperation():
 p = subprocess.Popen(["df","-Ph"], stdout=subprocess.PIPE)
 p2 = subprocess.Popen(["awk",'NR>=2 {print $6","$5","$4}'], stdin=p.stdout, stdout=subprocess.PIPE, universal_newlines=True)
 p.stdout.close()
 out,err = p2.communicate()
 print(out)


 linuxOperation()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.