0

I have a simple function that simply prints "Hi!". I want to use bash to call my function, instead of lets say IDLE. However bash doesn't seem to want to print the output returned from the hi(): function.

    #!/usr/bin/python
    def hi():
        print 'Hi!'

This doesn't print "Hi!", when I type python hi.py (or ./hi.py) into bash

However if I do not include the print statement inside of a function, but just inside the file hi.py as a lone print 'Hi!' statement; then bash does output text "Hi!" accordingly. From bash this code below outputs Hi!

    #!/usr/bin/python
    print 'Hi!'

From within bash, how might I make bash output the string from the function hi(): in the file hi.py?

Thanks

Skillionaire
  • 83
  • 2
  • 6
  • 1
    You need to call the function. e.g. add `hi()` at the end of your python script. (this is frequently done in an [`if __name__ == '__main__':`](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) suite...) – mgilson Jan 20 '17 at 00:07
  • If you really want to run the specific function(and not the module), see [this](http://stackoverflow.com/questions/2119702/calling-a-python-function-from-bash-script) question. However, it seems like what you want is what @mgilson suggested. – Albert Rothman Jan 20 '17 at 00:12
  • Thanks and sorry I had to reword my title. – Skillionaire Jan 20 '17 at 00:13
  • I literally love stack overflow and its valued contributors. I aspire to be as helpful as you all one day, so I can help others too. Thanks thanks thanks a millie. – Skillionaire Jan 20 '17 at 00:16

3 Answers3

0

You need to do this in you hi.py:

  def hi():
      print 'Hi!'

  if __name__ == '__main__':
      hi()

Then it will work in bash:

#!/bin/bash
python hi.py
Jiaxi
  • 26
  • 3
  • This solution worked for me. Thanks. But how does this work? So my understanding is that __name__ is a variable (of the hi(): function even though I didn't declare it) which is compared to value '__main__' (another variable of the hi(): function or basically all functions). If they're equal then hi() function is called. – Skillionaire Jan 20 '17 at 00:42
  • This is acting like the main method. Like java, when you have defined a function, you will need to create the main function (_public static void main()_) for calling that function. Now what happened here is when you using **python hi.py** in command line, it assumes that the code you run contains the main method which has the name '\_\_main\_\_'. Therefore, when '\_\_main\_\_' is detected, the hi() function will be called. – Jiaxi Jan 23 '17 at 02:25
  • Thanks for your clarification as to python expecting to find the __main__ method. I am familiar with Java syntax so the analogy hit home. Ive been learning python programming using Harvey Mudd College's CS For All online book available here https://www.cs.hmc.edu/csforall/index.html. Their path toward OOP in python takes the reader through various programming paradigms (i.e. functional programming and imperative) to teach Python syntax and semantics; before reaching OOP. Thus I hadnt reached classes yet, and was unfamiliar with the __main__ function. Thanks. – Skillionaire Feb 03 '17 at 03:47
0

Are you calling the function in your python script? In order to get the print statement in your function to be activated you actually need to call the function. For example running python test.py for this file prints "Hi!"

# Program named test.py
def output():
    print('Hi!')

output() # Calling the output function which will cause a print statement

Also calling this python file in bash works also. For example calling bash call.sh for this script worked and printed "Hi!" to the command line

#!/bin/bash
# Script to call python file
python test.py
arie64
  • 572
  • 6
  • 10
  • Thanks this solution works as well. Basically as you said above I must call the function( its name output()) from within the module itself, so python can allow bash to output the string. Its getting clearer thanks. Above your post @Jiaxi advised a similar solution with the addition of checking the hi(): functions variable __name__ against the value '__main__'. These are essentially similar solution but his uses the if condition. Thank you. – Skillionaire Jan 20 '17 at 00:53
0

Assuming that you want to execute a particular function.The python executable accepts the '-c' flag to indicate that you are passing it code. So if my file (hi.py) is:

def hi():
        print 'Hi!'

Then I could do:

$ python -c "execfile('hi.py'); hi()"

which pints the output

HI!

Reason

when you execute a python script you should tell it where to start.Since Hi is not a main function you need to manually call hi()

def hi():
        print 'Hi!'
hi()