Python Functions

ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
I am working on a Linux box doing some dabbling with python and wish to know how to run the following scripts in from the BASH Prompt

Code example 1

#!/usr/bin/python
def pyfunc()
print "Hello function"


Code example 2

#!/usr/bin/python
# System information Gathering Script
import subprocess

#Command 1
def uname_func();

uname = "uname"
uname_arg = "-a"
print "Gathering System Information with %s command:\n % uname
subprocess.call([uname, uname_arg])

def disk_func():



If I run the code i.e hello.py nothing happens, Regarding the second example I have intentionally left the second funtion blank however is there a way to first run the functions seperatley from BASH i.e run the uname_func() secondly is there a way of running all the functions at once.

Thanks for helping me out :) I am a python noob trying to get my head around this
Microsoft's strategy to conquer the I.T industry

" Embrace, evolve, extinguish "

Comments

  • ccnxjrccnxjr Member Posts: 304 ■■■□□□□□□□
    Ahh,

    For Code Example 1
    1) Colon after the parenthesis in your function
    def pyfunc():
    
    2) After you define your function you need to call it in the body
    #!/usr/bin/python
    def pyfunc():
        print "Hello function"
    
    pyfunc()
    

    3) Make sure you set the file as an executable
    chmod +x pyfunc
    

    I'll leave the second example to more knowledgeable persons....
    I do know this much, you could install IPython, which basically gives you a Python shell, ie, python on top of your vanilla bash.
    Sorta like the IDE, but you can run normal bash commands as well (such as "ls" , "chmod", "grep" etc.

    As far as a calling each function individually, you could pass arguments to the script to indicate which, if any of the functions to run or run them all.
    Maybe a bunch "case" statements based on the arguments passed.

    Running them all at once.... I think using sub shells might be the best approach, you could spawn subshells from within Python (I forget how..... I think using the PExpect library? )
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Hey thanks for the reply I downloaded Canopy and wrote the following

    #!/usr/bin/env python
    # System Gathering Script

    ##################################################################

    import subprocess

    # Command 1
    def disk_function():
    diskspace = "df"
    diskspace_arg = "-h"
    print "Gathering diskspace information %s command:\n" % diskspace
    subprocess.call([diskspace, diskspace_arg])

    # Command 2
    def system_services():
    service = "chkconfig"
    print "Showing Running Services %s command:\n" % service
    subprocess.call([service])

    # Main Function that calls other functions
    def main():
    disk_function()
    system_services()
    main()

    Runs perfectly fine :) only basic stuff but you gotta start from somewhere right lol :)
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • gbdavidxgbdavidx Member Posts: 840
    ally_uk wrote: »
    Hey thanks for the reply I downloaded Canopy and wrote the following

    #!/usr/bin/env python
    # System Gathering Script

    ##################################################################

    import subprocess

    # Command 1
    def disk_function():
    diskspace = "df"
    diskspace_arg = "-h"
    print "Gathering diskspace information %s command:\n" % diskspace
    subprocess.call([diskspace, diskspace_arg])

    # Command 2
    def system_services():
    service = "chkconfig"
    print "Showing Running Services %s command:\n" % service
    subprocess.call([service])

    # Main Function that calls other functions
    def main():
    disk_function()
    system_services()
    main()

    Runs perfectly fine :) only basic stuff but you gotta start from somewhere right lol :)

    your right man, that is great stuff, i with my local community college taught python instead of java
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    I am trying to get more structured about learning python

    I am working my way through Code Academy at the moment. Does anybody know any other good resources i'm particularly interested in using Python for administration so any good resources along those lines would be great. ( Similar to Bash scripts)
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • gbdavidxgbdavidx Member Posts: 840
    what about coursera? they have a free course on python
  • W StewartW Stewart Member Posts: 794 ■■■■□□□□□□
    I haven't messed with python in awhile(been using perl a lot lately) but I believe you want to pass a list/array to the subprocess module when you're command has arguments.

    Edit: That may just be for when you have more than one argument.
  • W StewartW Stewart Member Posts: 794 ■■■■□□□□□□
    ally_uk wrote: »
    I am trying to get more structured about learning python

    I am working my way through Code Academy at the moment. Does anybody know any other good resources i'm particularly interested in using Python for administration so any good resources along those lines would be great. ( Similar to Bash scripts)

    tutorials point has some great tutorials. I've always thought it was important to know how to interpret man pages and now I'm starting to realize that it can be useful to be able to interpret official documentation on a particular programming language as well. It's helped me be able to put together flash code when I couldn't find any examples to go off of.
  • YFZbluYFZblu Member Posts: 1,462 ■■■■■■■■□□
    Nice, I like the idea of a college teaching Python over Java. Now if only I could find a local school that teaches C over C++

    The trouble with using Python to run Linux commands is that Bash is clearly the better tool for the job.
  • Antonio72Antonio72 Member Posts: 29 ■□□□□□□□□□
    For the next week, stacksocial is giving away a free 45 day trial of skillfeed.com. You will have to put in your credit card info when you sign up with skillfeed so remember to set up a reminder to cancel it just in case, I have a habit of forgetting things so that was the first thing I did haha. I signed up already and there are a ton of courses. I did a quick search and it looks like there are about 10 courses on Python. I just set up my first LAMP server last night with the help of one of the courses so I'm definitely enjoying my experience so far. Good luck and hope this helps.
Sign In or Register to comment.