0

We want to run the following shell command by python script ( we use python version 2.7 )

echo hadoop-hdfs-namenode - 2.6.4.0-91| grep hadoop-hdfs-namenode | awk '{print $NF}' | awk '{printf "%.1f\n", $NF}'
2.6

So I create the following python script to get the results - 2.6

import os

os.system("echo hadoop-hdfs-namenode - 2.6.4.0-91| grep hadoop-hdfs-namenode | awk '{print $NF}' | awk '{printf "%.1f\n", $NF}' ")

but when I run it we get

 os.system("echo hadoop-hdfs-namenode - 2.6.4.0-91| grep hadoop-hdfs-namenode | awk '{print $NF}' | awk '{printf "%.1f\n", $NF}' ")
                                                                                                                        ^
SyntaxError: invalid syntax

Is it possible to run this complicated shell via python ? , in order to get the expected results - 2.6

And how to fix my syntax?

4
  • 2
    You could also use the newer subprocess module that is intended to replace os.system calls. Commented Jul 8, 2020 at 11:20
  • in addition to what @mabergerx said its always a good idea to check return codes from each step so you know when an error happens Commented Jul 8, 2020 at 11:23
  • change {printf "%.1f\n", $NF} to {printf '%.1f\n', $NF} Commented Jul 8, 2020 at 11:24
  • I get this after the update per you recommendation - awk: cmd. line:1: {printf %.1f awk: cmd. line:1: ^ syntax error awk: cmd. line:1: {printf %.1f awk: cmd. line:1: ^ unexpected newline or end of string sh: line 1: , $NF}: command not found Commented Jul 8, 2020 at 11:31

1 Answer 1

4

escape " and \n : os.system("echo hadoop-hdfs-namenode - 2.6.4.0-91| grep hadoop-hdfs-namenode | awk '{print $NF}' | awk '{printf \" %.1f\\n \", $NF}' ") .

As a side note os.system will execute the command (a string) in a subshell and return the return code of the command , if you need the output take a look at the subprocess module: https://docs.python.org/3/library/subprocess.html

Sign up to request clarification or add additional context in comments.

2 Comments

I get this error - awk: cmd. line:1: {printf "%.1f awk: cmd. line:1: ^ unterminated string awk: cmd. line:1: {printf "%.1f awk: cmd. line:1: ^ syntax error
Be careful you need to escape : os.system("echo hadoop-hdfs-namenode - 2.6.4.0-91| grep hadoop-hdfs-namenode | awk '{print $NF}' | awk '{printf \" %.1f\\n \", $NF}' ")

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.