Learn Python Control Flow and Loops to Write and Tune Shell Scripts – Part 2

Page 1 of 212

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

6 Responses

  1. Paul Campbell says:

    The web version does not include indentation where required by python syntax. I added the indentation and got past those errors and then got
    the following:

    SyntaxError: Non-ASCII character ‘\xc3’ in file uname.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

    Also it will not run on windows because os.uname not available except on linux.

    I got around these errors by using pythonanywhere.com on the web. It ran and was neat but I would think some one at tecmint could help with
    the indentation problem and also specify it will not work on Windows.

    • Gabriel A. Cánepa says:

      @Paul,
      Thanks for bringing this to our attention. I must confess I never thought I had to state explicitly this would work on Linux only, but we will add that notice as per your suggestion.
      @Ravi,
      Please add a small note to clarify. As for the indentation, perhaps we will need a different WP plugin. Any ideas?

  2. big joe says:

    Program will not run as shown due to no indentation starting with ‘if len(item) < len(headers[index]):'.

    • Gabriel A. Cánepa says:

      @big joe,
      It’s possible that the web formatting has damaged the indentation a little. But I can assure you that the program works, as you can see in the screenshots.

  3. Andy F says:

    I prefer to use subprocess.Popen to execute Linux commands:
    uptime = subprocess.Popen([‘uptime’, ‘-s’], stdout=subprocess.PIPE).communicate()[0]

    I would also recommend O’Reilly’s “Python for Unix and Linux System Administration”, by Noah Gift & Jeremy Jones. You can easily find .pdfs (if you don’t want to buy it).

    • @Andy,
      While that is true, on a personal note I prefer to keep things as simple as possible. If there is a Python-native way of executing a given command without spawning new process, I’d usually stick with it. Thank you for the book recommendation, I’ll take a look at it.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

Join Over 300K+ Linux Users
  1. 177,942
  2. 8,310
  3. 37,548

Are you subscribed?